我有一个项目,我们称之为Sample.它是一个针对.NET Framework 4.5.1的普通类库.然后,我在解决方案中有另一个项目,我们称之为Sample.CoreFx.它是一个.NET核心项目,基本上只包含一个project.json文件(当然还有生成的Sample.CoreFx.xproj文件).project.json看起来像这样:
{
"version": "1.0.0-*",
"compile": "..\\Sample\\**\\*.cs",
"frameworks": {
"dotnet": {
"dependencies": {
...
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
也许我还应该注意,我没有为我的Sample.CoreFx项目使用模板,但只是创建了project.json文件,并在Visual Studio中右键单击了我的解决方案并使用Add -> Existing Project并选择了它.
现在,我的问题是:在Visual Studio 2015中,左上角有一个下拉列表,允许切换上下文.在我的例子中,它显示了两个条目:Sample和Sample.CoreFx..NET Platform.但是,当我选择后一个并再次单击编辑器时,选择将更改为Sample.这导致我没有.NET Core上下文的IntelliSense.有谁知道为什么会出现这种情况?
我想创建一个加载服务,为枚举中定义的 ID 返回正确类型的数据。我所做的看起来像这样:
enum IdentifierEnum {
ID1 = 'ID1',
ID2 = 'ID2'
}
interface DataType {
[IdentifierEnum.ID1]: number,
[IdentifierEnum.ID2]: string
}
class LoadingService {
loadData<K extends IdentifierEnum>(key: K): DataType[K] {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
使用此方法,在使用加载服务时可以正确推断类型:
const loadingService: LoadingService = new LoadingService();
const data1 = loadingService.loadData(IdentifierEnum.ID1); // type: number
const data2 = loadingService.loadData(IdentifierEnum.ID2); // type: string
Run Code Online (Sandbox Code Playgroud)
我面临的唯一问题是,在 的实现中loadData,类型参数K仅被推断为IdentifierEnum。因此,以下内容将不起作用:
class LoadingService {
loadData<K extends IdentifierEnum>(key: K): DataType[K] {
if (key === IdentifierEnum.ID1) {
return 1; // …Run Code Online (Sandbox Code Playgroud)