C# 如何通过反射将我创建的类型区分为系统类型?

C# 如何通过反射将我创建的类型区分为系统类型?,c#,reflection,C#,Reflection,我想知道我传递的类型是系统类型还是我创建的类型。我怎么知道?看: // 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

我想知道我传递的类型是系统类型还是我创建的类型。我怎么知道?看:

// 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...
    }
}
验证这一点的最佳方法是什么

OBS:在由Microsoft签名的程序集中,将所有类型的核心标准库计算为“系统类型”,例如:DateTime、String、Int32、Boolean(mscorlib.dll | system.dll中的所有类型)

OBS2:我的实体不会从那些“系统类型”继承

OBS3:我的实体可以是我创建的任何类型,因此我无法在比较中指定

OBS4:我需要在不指定是否等于字符串、布尔值的情况下进行比较…

什么是“系统”类型?您可以检查是否:

  • 在mscorlib中
  • 它位于由Microsoft签名的程序集中
  • 它是一套固定类型中的一种,你事先认为它是“系统”
  • 它位于一组固定的组件中,您事先认为它们是“系统”
  • (很容易伪造)其名称空间是
    System
    或以
    System开头。
一旦定义了“系统”的含义,这就相当于建议使用用于检查它的代码。例如:

  • if(type.Assembly==typeof(string.Assembly)
  • var publisher=typeof(string.Assembly.Evidence.GetHostEvidence()-然后检查发行商是否拥有适用于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))
{
    ...
}
private static readonly readonly集合系统程序集=
新名单{
typeof(string).Assembly,//mscorlib.dll
typeof(Process).Assembly,//System.dll
}.AsReadOnly();
...
if(SystemAssemblies.Contains(type.Assembly))
{
...
}

只需使用“property is yourclass”检查您自己的属性即可?定义系统类型。核心标准库中的类型?在任何引用的库中?只是出于好奇,您的用例是什么?System type,是核心标准库中的一种类型,在由Microsoft签名的程序集中。@ViniciusOttoni:什么算是“核心标准库”?确切地说,是哪个程序集?另一种可能:它的命名空间是
System
或嵌套在
System
@phoog中,因为它可以在
System
命名空间中编写自己的代码,但不确定它是否相关。@phoog:True,尽管用户仍然可以创建它。@ViniciusOttoni:如问题注释中所述,您需要准确地定义“核心标准库”所指的程序集-不仅要给出示例,还要给出一个全面的列表。例如,您是否需要客户端配置文件中未包含的任何程序集?或者只是mscorlib.dll和System.dll?你需要精确——不仅是在沟通上,而且是在思维上——在这一点上,这是相当简单的。@AfnanBashir:我在这里和那里挑选东西——但实际上,这不是知识的问题,而是传递知识的能力的问题。我似乎在沟通方面做得很好,虽然我不能确切地告诉你如何或为什么。。。