如何通过反射区分我创建的类型和系统类型?
我想知道我传递的类型是系统类型还是我创建的类型。我怎么知道这个?看看:
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
什么算作“系统”类型?您可以检查是否:
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))
- 一旦您提出了自己的系统程序集列表(更实用)编辑:根据评论,如果您对
mscorlib
和System.dll
感到满意:What counts as a "system" type? You could check whether:
System
or starts withSystem.
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 forpublisher
having the right certificate for Microsoftif (SystemTypes.Contains(type))
- once you've come up with your own list of system typesif (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
andSystem.dll
: