使用C#中的反射查找属性类型。

huangapple go评论90阅读模式
英文:

find type of property with relection in C#

问题

我有这个模型:

public class model
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Family { get; set; }
    public int RoleId { get; set; }
    public Role Role { get; set; }
}

public class Role
{
    public int RoleId { get; set; }
    public string RoleName { get; set; }
    public string SEC { get; set; }
}

我需要在模型中搜索并找到类型为Class的属性,但无法找到它们。

我尝试了这段代码:

PropertyInfo[] props = model.GetType().GetProperties();
foreach (var item in props)
{
    if (item.PropertyType.IsClass)
    {
        PropertyInfo[] subClassProp = item.GetType().GetProperties();
        foreach (var itemB in subClassProp)
        {
            selectList.Add(itemB.Name);
        }
     }
}

我该如何解决这个问题?

英文:

i have this model :

public class model
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Family { get; set; }
    public int RoleId { get; set; }
    public Role Role { get; set; }
}

public class Role
{
    public int RoleId { get; set; }
    public string RoleName { get; set; }
    public string SEC { get; set; }
}

i need search in the model and find propertys of type is Class but it can not find them .

i try by this code :

PropertyInfo[] props = model.GetType().GetProperties();
foreach (var item in props)
{
    if (item.PropertyType.IsClass)
    {
        PropertyInfo[] subClassProp = item.GetType().GetProperties();
        foreach (var itemB in subClassProp)
        {
            selectList.Add(itemB.Name);
        }
     }
}

How Can i Solve This Problem ?

答案1

得分: 2

以下是翻译的部分:

// 获取model类型的公共实例属性
PropertyInfo[] props = typeof(model).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var item in props)
{
   // 如果属性的类型是类且与model类型的程序集相同
   if (item.PropertyType.IsClass && item.PropertyType.Assembly.FullName == typeof(model).Assembly.FullName)
	{
	        // 获取属性的公共实例属性
	        PropertyInfo[] subClassProp = item.PropertyType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
	        foreach (var itemB in subClassProp)
	        {
	            // 在此处编写您的代码
	        }
	}
}

在原始代码中,以下代码尝试获取PropertyInfo的属性:

item.GetType().GetProperties();

而您实际需要使用以下代码:

item.PropertyType.GetProperties();

此外,您可以通过使用BindingFlags来仅关注公共实例属性:

.GetProperties(BindingFlags.Public | BindingFlags.Instance);

还需要考虑的一种情况是字符串被视为类。出于同样的原因,如果您的意图是仅捕获用户定义的类,则可以使用以下条件:

item.PropertyType.Assembly.FullName == typeof(model).Assembly.FullName
英文:

Believe you are attempting to do the following.Explanation is given below the code

PropertyInfo[] props = typeof(model).GetProperties(BindingFlags.Public|BindingFlags.Instance);
foreach (var item in props)
{
   if (item.PropertyType.IsClass && item.PropertyType.Assembly.FullName == typeof(model).Assembly.FullName)
	{
	        PropertyInfo[] subClassProp = item.PropertyType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
	        foreach (var itemB in subClassProp)
	        {
	            // Your code 
	        }
	}
}

In the code given in OP, Following code was attempting to get Properties of PropertyInfo

item.GetType().GetProperties();

Instead you need to use

item.PropertyType.GetProperties();

Further, you can focus on only the public instance properties by using the Binding Flags

.GetProperties(BindingFlags.Public|BindingFlags.Instance);

Another case to be considered is that String would be considered as Class. For the same reason, if you intention is to capture only the User Defined Classes, you could use following condition

item.PropertyType.Assembly.FullName == typeof(model).Assembly.FullName

huangapple
  • 本文由 发表于 2020年1月3日 14:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定