none
C#反射问题,大家帮帮忙,急 RRS feed

  • 问题

  • C#中如何用反射获取属性成员中的属性,例如:
    class B
    {
      private string strB;
    }

    class Target
    {
      private B member = new B();
    }

    public Target targetInstance = new Target();

    我现在想用反射直接得到targetInstance.member.strB这个属性,应该怎么做,我试过type.GetProperty("member"), 但是这个是返回member属性, type.GetProperty("member.strB")这个行不通。

    请教一下应该如何去取?最好是用一次反射就直接取到strB。多谢!
    2011年10月25日 13:09

答案

  • Hi FlashBrother,

    欢迎来到MSDN论坛!

    请您参考下面的解决方案:

     

        class Program
        {
            static void Main(string[] args)
            {
                Assembly assembly = Assembly.LoadFrom(@"ClassLibrary1.dll");
              
               Type[] ts = assembly.GetTypes();                     
     
               foreach (Type t in ts)
               {                  
                   MemberInfo [] f = t.GetMember("strB",BindingFlags.DeclaredOnly | BindingFlags.Instance |  BindingFlags.NonPublic);
                   if (f != null)
                       foreach (var fi in f)
                           Console.WriteLine(fi.Name);                     
               }
            }
        }
      
       namespace ClassLibrary1
       {
           public class Class1
           {
               public class B
               {
                   private string strB;
               }
     
               public class Target
               {
                   private B member = new B();
               }
               public Target targetInstance = new Target();
           }
       }
    

    祝您一切顺利!


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年10月27日 6:00
    版主

全部回复

  • class B 和 class  Target 一致的。

    既然可以获取 member , 那么再来一次,针对于 member ,类似 针对于 targetInstance。

    // 呵呵。

    2011年10月26日 1:55
  • Hi FlashBrother,

    欢迎来到MSDN论坛!

    请您参考下面的解决方案:

     

        class Program
        {
            static void Main(string[] args)
            {
                Assembly assembly = Assembly.LoadFrom(@"ClassLibrary1.dll");
              
               Type[] ts = assembly.GetTypes();                     
     
               foreach (Type t in ts)
               {                  
                   MemberInfo [] f = t.GetMember("strB",BindingFlags.DeclaredOnly | BindingFlags.Instance |  BindingFlags.NonPublic);
                   if (f != null)
                       foreach (var fi in f)
                           Console.WriteLine(fi.Name);                     
               }
            }
        }
      
       namespace ClassLibrary1
       {
           public class Class1
           {
               public class B
               {
                   private string strB;
               }
     
               public class Target
               {
                   private B member = new B();
               }
               public Target targetInstance = new Target();
           }
       }
    

    祝您一切顺利!


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年10月27日 6:00
    版主