【问题标题】:Reflection C# question反射 C# 问题
【发布时间】:2009-09-28 22:20:08
【问题描述】:

我在获取一个属性是另一个类的类的值时遇到问题。

这是一个例子:

public class Person 
{
    private int age;
    private string name;

    public Person()
    {
        Address = new Address();

    }

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public Address Address { get; set; }
}

public class Address
{
    public string street { get; set; }
    public string houseno { get; set; }
}


public class Program
{
    static void Main(string[] args)
    {

        Person person = new Person();
        person.Age = 27;
        person.Name = "Fernando Vezzali";
        person.Address.houseno = "123";
        person.Address.street = "albert street";

        Type type = typeof(Person);

        PropertyInfo[] properties = type.GetProperties();
        foreach (PropertyInfo property in properties)
        {
            Console.WriteLine("{0} = {1}", property.Name, property.GetValue(person, null));

        }
   }
}

但是有了这个我没有得到地址的值。

有人可以帮忙吗?

【问题讨论】:

    标签: reflection c#-2.0


    【解决方案1】:

    考虑到 Jason 的回答,这是可能的 ToString...

    您还可以将返回的反射对象转换为地址以访问完整的对象和属性

    public class Address
    {
        public string street { get; set; }
        public string houseno { get; set; }
        public override ToString() {
            return string.Format("street: {0}, house: {1}", street, houseno);
        }
    }
    

    【讨论】:

      【解决方案2】:

      type.GetProperties() 只获取 that 类型的属性,其中之一是对象Addressstreethouseno 不是 Person 的属性。

      Console.Write... 在每个参数上隐式调用ToString()。所以你可能会看到“Address - Namespace.Address”作为输出,因为someAddressObject.ToString() 将返回类型名称。

      在这种特定情况下获得所需内容的最简单方法是在 Address 对象上覆盖 ToString() 以输出对象的一些有意义的字符串表示形式:

      public override ToString()
      {
          return string.Format("#{0} {1}",
              this.houseno,
              this.street); //returns #123 StreetName
      }
      

      如果您确实需要在对象上编写每个子对象的每个属性,那可能会变得相当复杂 - 您实际上是在谈论序列化,它会沿着对象树递归到每个对象中。

      【讨论】:

        【解决方案3】:

        您需要在 Address 中实现 ToString(),如果您对返回格式化字符串作为 Address 的值感到满意,或者您的迭代代码需要检查每个属性以确定该属性的类型是否也公开属性,并且将其排入队列以供进一步检查。

        【讨论】:

          【解决方案4】:

          您的 foreach 正在正确地遍历所有属性,我相信它隐含地调用 ToString 以获取值,因此覆盖您的 Address 类的 ToString 方法,并将属性作为字符串返回。

          或者,在 foreach 中,通过获取属性类型并检查 IsValueType 或 IsClass 来测试您的属性是值类型还是类类型。如果 IsValueType 为 false,则遍历该属性的类类型的属性,就像您对 Person 的属性所做的那样。

          类似这样的东西(你可能需要 tweek 来编译它,但它给了你这个想法):

          Person person = new Person();        
          person.Age = 27;        
          person.Name = "Fernando Vezzali";        
          person.Address.houseno = "123";        
          person.Address.street = "albert street";        
          
          Type type = person.GetType();        
          
          PropertyInfo[] properties = type.GetProperties();        
          foreach (PropertyInfo property in properties)        
          {
              //get the type of this property
              Type tyProperty = property.PropertyType;
          
              object oValue = property.GetValue(person, null));        
          
              //if the property is a value
              if (tyProperty.IsValueType)
              {
                  Console.WriteLine("{0} = {1}", property.Name, oValue);
              }
              else  //else if property type is a class
              {
                  oSubValue = property.GetValue(oValue, null)); 
          
                  //loop through the classes properties
                  PropertyInfo[] lstSubProperties = tyProperty.GetProperties();        
                  foreach (PropertyInfo propSub in lstSubProperties)        
                  {
                       Console.WriteLine("{0} = {1}", propSub .Name, oSubValue);  
                  }
              }
          }   
          

          【讨论】:

          • 嗨 Jeremy,这就是我想要做的,但如何去做,这是我的问题,我可以检查它的类,但无法获取该属性(地址)的每个属性。跨度>
          猜你喜欢
          • 2010-11-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多