如何通过反射区分我创建的类型和系统类型?

发布于 2025-01-08 17:33:14 字数 600 浏览 0 评论 0原文

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

// 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:我需要进行比较而不指定是否等于字符串、布尔值...

I would like to know if a type that i pass is a system type or a type that I created. How can i know this? Look:

// 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...
    }
}

What is the best way to verify that?

OBS: Counts as a "system type" all types of the core standard library, in an assembly signed by Microsoft like: DateTime, String, Int32, Boolean (all types in mscorlib.dll | System.dll)...

OBS2: My entities will not inherit from those "system types".

OBS3: My entity could be any type that I created, so I cannot specify in the comparison.

OBS4: I need to do the comparison without the specifying if the the is equal to String, Boolean...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨夜星沙 2025-01-15 17:33:14

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

  • 它在 mscorlib 中
  • 它在由 Microsoft 签名的程序集中 它
  • 是您预先认为是“系统”的一组固定类型之一
  • 它是在您认为是的一组固定程序集之一中事先是“system”
  • (很容易伪造)它的命名空间是System或以System开头。

一旦你定义了“system”的含义,这几乎表明了代码用来检查它。例如:

  • if (type.Assembly == typeof(string).Assembly)
  • varpublisher = typeof(string).Assembly.Evidence.GetHostEvidence(); - 随后检查 publisher 是否拥有 Microsoft 的正确证书
  • if (SystemTypes.Contains(type)) - 一旦您出现使用您自己的系统类型列表
  • if (SystemAssemblies.Contains(type.Assembly)) - 一旦您提出了自己的系统程序集列表(更实用)

编辑:根据评论,如果您对 mscorlibSystem.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))
{
    ...
}

What counts as a "system" type? You could check whether:

  • It's in mscorlib
  • It's in an assembly signed by Microsoft
  • It's one of a fixed set of types you deem to be "system" beforehand
  • It's in one of a fixed set of assemblies you deem to be "system" beforehand
  • (Easy to fake) Its namespace is System or starts with System.

Once you've defined what you mean by "system", that pretty much suggests the code used to check it. For example:

  • if (type.Assembly == typeof(string).Assembly)
  • var publisher = typeof(string).Assembly.Evidence.GetHostEvidence<Publisher>(); - followed by a check for publisher having the right certificate for Microsoft
  • if (SystemTypes.Contains(type)) - once you've come up with your own list of system types
  • if (SystemAssemblies.Contains(type.Assembly)) - once you've come up with your own list of system assemblies (more practical)

EDIT: As per comments, if you're happy with just mscorlib and 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))
{
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文