c#反射获取属性的类型怎么实现

avatar
作者
筋斗云
阅读量:0

在C#中,可以使用Reflection来获取属性的类型。具体步骤如下:

  1. 使用Type.GetType()方法获取属性所在的类的Type对象。

  2. 使用GetType().GetProperty()方法获取属性的PropertyInfo对象。

  3. 使用PropertyInfo.PropertyType属性获取属性的类型。

以下是一个示例代码:

using System; using System.Reflection;  public class MyClass {     public int MyProperty { get; set; } }  class Program {     static void Main()     {         Type type = typeof(MyClass);         PropertyInfo propertyInfo = type.GetProperty("MyProperty");                  Type propertyType = propertyInfo.PropertyType;                  Console.WriteLine("Property type: " + propertyType);     } } 

在上面的示例中,我们首先获取了MyClass类的Type对象,然后使用GetProperty()方法获取了MyProperty属性的PropertyInfo对象,最后通过PropertyType属性获取了属性的类型。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!