.NET反射 - 从实例属性中获取声明类类型

是否可以从属性实例中获取类的类型 我尝试了以下内容
var model = new MyModel("SomeValueForMyProperty")

Type declaringType = model.MyProperty.GetType().DeclaringType
但结果始终不适用于DeclaringType和ReflectedType     
已邀请:
Type
到宣布该类型属性的类没有直接链接。 你需要使用
PropertyInfo
PropertyInfo propInfo = model.GetType().GetProperty("MyProperty");

// get the property value:
object value = propInfo.GetValue(model, null);
// get the property's declaring type:
Type declaringType = propInfo.DeclaringType;
    

要回复问题请先登录注册