我如何确定一个属性是在C#用户定义类型? [英] How do I determine if a property is a user-defined type in C#?

查看:377
本文介绍了我如何确定一个属性是在C#用户定义类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何确定一个属性是用户定义类型?我试图用IsClass,如下图所示,但它的价值是字符串的属性为真(和谁知道还有什么)。

How do I determine if a property is a user-defined type? I tried to use IsClass as shown below but its value was true for String properties (and who knows what else).

foreach (var property in type.GetProperties()) {
    if (property.PropertyType.IsClass) {
        // do something with property
    }
}

*更新更加清晰*

我想遍历一个给定类型的定义,如果给定类型或者其任何公共属性都装配中定义的,我正在寻找一个嵌入的JavaScript文件。我只是不想浪费在本地.NET类型的处理资源和时间。

I am trying to traverse a given type's definition and if the given type or any of its public properties are defined within the assembly, I am searching for an embedded JavaScript document. I just don't want to waste processing resources and time on native .NET types.

推荐答案

@Bobson做一个真正的好点

@Bobson made a really good point:

......不像一些其他的语言,C#不作之间的任何实际的
区分用户自定义,并标准类型

"...Unlike some other languages, C# does not make any actual distinction between "user-defined" and "standard" types."

从技术上讲,@Bobson答曰;有一个在.NET框架或其他任何集会为此事定义的用户定义类型和1之间无显着差异。

Technically, @Bobson gave the answer; there is no distinguishing difference between a user-defined type and one defined in the .NET Framework or any other assembly for that matter.

不过,我发现了几个有用的方法。确定一个类型是用户定义的

However, I found a couple useful ways to determine if a type is user-defined.

要搜索特定类型的组件中定义的所有类型,这个伟大的工程:

To search for all types defined within the given type's assembly, this works great:

foreach (var property in type.GetProperties()) {
    if (property.PropertyType.IsClass 
    && property.PropertyType.Assembly.FullName == type.Assembly.FullName) {
        // do something with property
    }
}

如果该类型可以在不同的组件来定义,但不包括在系统命名空间的作品在大多数情况下:

If the types can be defined in various assemblies, excluding the System namespace works in most cases:

foreach (var property in type.GetProperties()) {
    if (property.PropertyType.IsClass 
    && !property.PropertyType.FullName.StartsWith("System.")) {
        // do something with property
    }
}

这篇关于我如何确定一个属性是在C#用户定义类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆