我想知道我传递的类型是系统类型还是我创建的类型.我怎么知道这个?看:
// Obs: currentEntity can be any entity that i created
var currentProperties = currentEntity.GetType().GetProperties();
foreach (var property in currentProperties)
{
if (/* Verify here if the property is a system type */)
{
// Do what i want...
}
}
Run Code Online (Sandbox Code Playgroud)
验证这个的最佳方法是什么?
OBS:作为"系统类型"计算所有类型的核心标准库,在由Microsoft签署的程序集中,如:DateTime,String,Int32,Boolean(mscorlib.dll中的所有类型| System.dll)...
OBS2:我的实体不会继承那些"系统类型".
OBS3:我的实体可以是我创建的任何类型,因此我无法在比较中指定.
OBS4:我需要进行比较而不指定它是否等于String,Boolean ...
什么算作"系统"类型?您可以检查是否:
System
或开始于System.
一旦你通过"系统"定义了你的意思,那几乎就是用来检查它的代码.例如:
if (type.Assembly == typeof(string).Assembly)
var publisher = typeof(string).Assembly.Evidence.GetHostEvidence<Publisher>();
- 然后检查是否publisher
拥有适用于Microsoft的正确证书if (SystemTypes.Contains(type))
- 一旦你想出了自己的系统类型列表if (SystemAssemblies.Contains(type.Assembly))
- 一旦你提出了自己的系统组件清单(更实用)编辑:按照意见,如果你满意的公正mscorlib
和System.dll
:
private static readonly ReadOnlyCollection<Assembly> SystemAssemblies =
new List<Assembly> {
typeof(string).Assembly, // mscorlib.dll
typeof(Process).Assembly, // System.dll
}.AsReadOnly();
...
if (SystemAssemblies.Contains(type.Assembly))
{
...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
95 次 |
最近记录: |