class A
{
 public string Property1{get;set;}
 public int Property2{get;set;}
}class B
{
 public string Property1{get;set;}
 public A Property2{get;set;}
}请问如何通过反射获取class B 的Property2属性(也就是class A)的 属性名和值?

解决方案 »

  1.   

                Assembly assembly = Assembly.LoadFrom(filename);
                Type t = assembly.GetType("B");
                Object obj = t.InvokeMember(
                null,
                BindingFlags.DeclaredOnly |
                BindingFlags.Public | BindingFlags.NonPublic |
                BindingFlags.Instance | BindingFlags.CreateInstance,
                null,
                null,
                null);            string strReturn = (string)t.InvokeMember("Property2",
                BindingFlags.DeclaredOnly |
                BindingFlags.Public | BindingFlags.NonPublic |
                BindingFlags.Instance | BindingFlags.GetProperty,
                null,
                obj,
                null);
    strReturn 就是你所要取得属性值。
      

  2.   

    B b = new B();            Type t = b.GetType().GetProperty("Property2").GetValue(b, null).GetType();
               
                foreach (PropertyInfo info in b.GetType().GetProperty("Property2").GetValue(b, null).GetType().GetProperties()) 
                {
                    MessageBox.Show(string.Format("{0}={1}",info.Name,info.GetValue(b.Property2,null)));
                }
      

  3.   

    Type type = typeof(B);
    B b = new B();
    PropertyInfo pi = type.GetProperty("Property2");
    A a = (A)pi.GetValue(b, null);
    ..............
      

  4.   

    @mooddecode 
    我没说清楚么?比如 
       A a = new A();
       a.Property1 = "property1OfA";
       a.Property2 = 1;
      B b = new B();
      b.Property1 = "property1OfB";
      b.Proerty2 = a;
    现在我想通过反射把对象b和a 的属性名和值打印出来
    不知道我说明白没
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    namespace WindowsApplication26
    {
        public partial class Form1 : Form
        {        class A
            {
                public string Property1 { get { return "Property1"; } }
                public int Property2 { get { return 1; } }
            }        class B
            {
                private A property2=new A();
                public string Property1 { get { return "Property1"; } }
                public A Property2 { get { return property2; } }
            } 
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {            B b = new B();           
                foreach (PropertyInfo info in b.GetType().GetProperty("Property2").GetValue(b, null).GetType().GetProperties()) 
                {
                    MessageBox.Show(string.Format("{0}={1}",info.Name,info.GetValue(b.Property2,null)));
                }
                        }
        }
    }
      

  6.   


     //print public properties 
            public void PrintProperties<T>(T t)
            {
                if (t == null)
                {
                    return;
                }            PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                if (properties.Length <= 0)
                {
                    return;
                }            foreach (PropertyInfo item in properties)
                {
                    if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
                    {
                        Console.WriteLine("{0}:{1}", item.Name, item.GetValue(obj, null));
                    }
                    else
                    {
                        //我不知道怎样写了
                    }
                }
    大家在帮看看吧
      

  7.   

    各位达人再帮see see 啊!
      

  8.   

    递归一下public void PrintProperties<T>(T t)
            {
                if (t == null)
                {
                    return;
                }            PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                if (properties.Length <= 0)
                {
                    return;
                }            foreach (PropertyInfo item in properties)
                {
                    string name = item.Name;
                    object value = item.GetValue(t, null);
                    if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
                    {
                        Console.WriteLine("{0}:{1}", name,value);
                    }
                    else
                    {
                        foreach (PropertyInfo itemsub in value.GetType().GetProperties())
                        {                        PrintProperties(value);
                        }
                    }
                }
            }
      

  9.   

    sorry,写错了 public void PrintProperties<T>(T t)
            {
                if (t == null)
                {
                    return;
                }            PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                if (properties.Length <= 0)
                {
                    return;
                }            foreach (PropertyInfo item in properties)
                {
                    string name = item.Name;
                    object value = item.GetValue(t, null);
                    if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
                    {
                        Console.WriteLine("{0}:{1}", name,value);
                    }
                    else
                    {                        PrintProperties(value);
                    }
                }
            }