简体   繁体   English

如何根据名称获取所有子类属性

[英]How do I get all subclass properties based on name

I have the following structure:我有以下结构:

 public class A
 {
    public A1Type A1{get;set;}
    public A2Type A2{get;set;}
    public A3Type A3{get;set;}
  }
  public A1Type
  {
    public string B1{get;set;}
    public string B2{get;set;}
  }

And similar with A1Type is also A2Type and A3Type.I have a more complex structure.和A1Type类似的还有A2Type和A3Type。我有一个更复杂的结构。

I receive a string parameter "A1Type" and I need to get from A just the subclass A1Type with its values.我收到一个字符串参数“A1Type”,我需要从 A 获取子类 A1Type 及其值。 Can somebody help me with this?有人可以帮我吗?

This might do what you want:这可能会做你想要的:

class Program
{
    static void Main(string[] args)
    {
        var a = new A()
        {
            A1 = new A1Type() { B1 = "A1Type B1 prop value", B2 = "A1Type B2 prop value" },
            A2 = new A2Type() { B1 = "A2type B1 prop value", B2 = "A2Type B2 prop value" },
            A3 = new A3Type() { B1 = "A3Type B1 prop value", B2 = "A3Type B2 prop value" }
        };
        ListPropertyValuesOf("A1Type", a);
        ListPropertyValuesOf("A2Type", a);
        ListPropertyValuesOf("A3Type", a);
    }

    static void ListPropertyValuesOf(string propName, A classA)
    {
        // get all properties of classA object
        var props = classA.GetType().GetProperties();
        foreach (var prop in props)
        {
            if (prop.PropertyType.Name == propName)
            {
                // get the specific sub-type object on classA
                var subClassA = classA.GetType().GetProperty(prop.Name).GetValue(classA);

                // loop each of the properties on subClassA and write its value
                foreach (var subProp in subClassA.GetType().GetProperties())
                    Console.WriteLine(subProp.GetValue(subClassA));
            }
        }
    }
}

public class A
{
    public A1Type A1 { get; set; }
    public A2Type A2 { get; set; }
    public A3Type A3 { get; set; }
}
public class A1Type
{
    public string B1 { get; set; }
    public string B2 { get; set; }
}

public class A2Type
{
    public string B1 { get; set; }
    public string B2 { get; set; }
}

public class A3Type
{
    public string B1 { get; set; }
    public string B2 { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我有类但子类名称为字符串时,如何从子类获取属性 - How do I get the properties from a subclass when I have the class but subclass name as a string 如何在C#.NET中为子类设置特定属性? - How do I set specific properties for a subclass in C# .NET? 如何获取子类属性并设置值? - how can I get subclass properties and set the value? 如何通过引用子类的实例来获取类的名称? - How can I get the name of a class through a reference to an instance of a subclass? 如何获取具有特定名称的所有节点 - How do I get all the nodes with a certain name 如果在C#中的字符串中有类型名称,如何获取对象的公共属性? - How do I get the public properties of an Object if I have the type name in a string in C#? 如何获取数据库表实体框架6的所有属性和属性显示名称? - how can i get all properties and attribute display name of database table entity framework 6? 当我使用 AutoMapper 时,如果这些属性具有相同的名称,我是否需要明确地编写所有属性? - Do I need to write all the properties explicitly when I am using AutoMapper, if these properties have the same name? 如何从类中获取名称为“Id”的所有 Guid 属性及其值? - How to get all Guid properties and their values with the name "Id" from class? 如何获取属性基于某种类型的类的所有属性 - How to get all properties of a class where the property is based on a certain type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM