2009-09-28 80 views
0

我有问题获取其中一个属性是另一个类的类的值。反射C#问题

下面是一个例子:

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)); 

     } 
    } 
} 

但是这个我不明白地址的值。

有人可以帮忙吗?

回答

2

type.GetProperties()只能获得的属性类型,其中之一是对象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 
} 

如果你确实需要编写的每个属性你的对象上的每个子对象都会变得相当复杂 - 你基本上是在谈论序列化,这个序列化是在一个对象树中递归并进入每个对象。

0

要么你需要在地址中实现ToString(),如果你满意地返回格式化字符串作为Address的值,或者你的迭代代码需要检查每个属性以确定该属性的类型是否也暴露属性,并将其排入后续检查。

2

这里是可能的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); 
    } 
} 
0

你的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); 
     } 
    } 
} 
+0

嗨杰里米, 那是什么我想做的事,但如何做到这一点,多数民众赞成我的问题 我可以检查它的类,但无法实现获得该属性(地址)的每个属性。 – Parminder 2009-09-30 21:46:23