通过反射区分阶级属性类型 [英] Distinguish class property types through reflection
问题描述
我有一个Rectangle类
I have a Rectangle class
public class Rectangle : Base, IRectangle
{
public IDimension dimension { get; set; }
public Position position { get; set; }
public String color { get; set; }
public int ID { get; set; }
public override String ToString()
{
return base.ToString(this);
}
}
是否有任何的方式,通过反射类型的属性来区分这对Rectangle类定义的?
Are there any way to distinguish through reflection types of properties which defined on Rectangle class?
我如何理解ID是结构或尺寸的接口?而这两个字符串和位置类,但字符串是建设类,位置是自定义类。
How can I understand ID is struct or dimension is Interface? And Both String and Position are class but String is build in class, Position is Custom class.
推荐答案
您可以使用此属性:
typeof(T).IsPrimitive
要检查,如果一个类型是基本或非原始
To check if a type is primitive or non-primitive
这一项:
typeof(T).IsInterface
要检查,如果一个类型是一个接口或没有。
To check if a type is an interface or not.
这是你如何检查是一种类型是一个结构或不:
This is how you check is a type is a struct or not:
typeof(T).IsValueType
在情况下,你是真正的寻找只为纯结构(一般不只是值类型),则:
In case you are truly looking only for "pure" structs (not just value types in general) then:
typeof(T).IsValueType && !typeof(T).IsEnum;
这篇关于通过反射区分阶级属性类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!