几个框架我有一个图书馆,在我的代码的一些点定标.NET Core
和net45
我需要使用反射像这样:编译器指令如何对使用.NET的核心
var type = obj.GetType();
var properties = type.GetProperties().ToList();
if (type.IsPrimitive || type.IsEnum || properties.Count == 0)
return new Dictionary<string, object> { { unnamed, obj } };
现在我移植我的图书馆也支持.net core
所以我做了一个新的.net core library
的项目,这是我的project.json
{
"version": "1.0.0",
"dependencies": {
"Wen.Logging.Abstractions": "1.0.0"
},
"frameworks": {
"net45": {
"frameworkAssemblies": {
"System.Reflection": "4.0.0.0"
},
"dependencies": {
"Newtonsoft.Json": "6.0.4",
"NLog": "4.3.5"
}
},
"netstandard1.6": {
"imports": "dnxcore50",
"dependencies": {
"NETStandard.Library": "1.6.0",
"System.Reflection.TypeExtensions": "4.1.0",
"Newtonsoft.Json": "8.0.2",
"NLog": "4.4.0-*"
}
}
}
}
增加我的阶级和编译器抱怨的type.IsPrimitive
和types.IsEnum
种性质,所以我想用编译器指令做这样的事情:
var type = obj.GetType();
var properties = type.GetProperties().ToList();
#if net45
if (type.IsPrimitive || type.IsEnum || properties.Count == 0)
return new Dictionary<string, object> { { unnamed, obj } };
#elif netstandard16
//... some code
#endif
一旦我做到这一点,net45
中的代码是灰色的(我想是因为VS是把它看作.net core
),但是不管是什么我在#elif
和#endif
之间设置的标签也显示为灰色。我也试图与#else
,然后我可以这样做:
var typeInfo = type.GetTypeInfo();
if(typeInfo.IsPrimitive || typeInfo.IsEnum || properties.Count == 0)
return new Dictionary<string, object> { { unnamed, obj } };
但是我的问题是:
什么标签我应该在我的代码中使用编译器指令#if
目标上同几个框架项目/库?
@JonSkeet帖子创建于一年前,所以这样的尝试是可以接受的。我同意今天没有人再这样做了。 –
@LexLi:好点 - 我只看到它,因为它是在3小时前编辑的。将删除我的评论... –