C# 如何返回虚拟属性PropertyInfo?
我在实体模型中返回虚拟值的属性时遇到困难,有人知道我如何返回该虚拟属性的PropertyInfo吗 我拥有以下实体: 实体C# 如何返回虚拟属性PropertyInfo?,c#,asp.net,reflection,propertyinfo,C#,Asp.net,Reflection,Propertyinfo,我在实体模型中返回虚拟值的属性时遇到困难,有人知道我如何返回该虚拟属性的PropertyInfo吗 我拥有以下实体: 实体 public class Company { public int Id { get; set; } public string Name { get; set; } public virtual Owner Owner { get; set; } } public class Owner { public int Id { get; set;
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Owner Owner { get; set; }
}
public class Owner
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
但是,在模型公司中返回Owner PropertyInfo时,我无法访问Owner模型的属性
基本示例:
public PropertyInfo GetPropertyInfo()
{
Type tType = typeof(Company);
PropertyInfo prop = tType.GetProperty("Owner.Name");
return prop;
}
变量prop返回null
我忘记实现什么了吗?您需要先获取
所有者
属性,然后通过它获取名称
:
var owner = tType.GetProperty("Owner");
var name = owner.PropertyType.GetProperty("Name");
如果您有权访问所有者
,也可以直接获取:
var name = typeof(Owner).GetProperty("Name");
为我工作很好!非常感谢。