none
通过反射 给属性中的类赋值 RRS feed

  • 问题

  • public class A

    {

    public string Name {get;set;}

    }

    public class B

    {

    public A a  {get;set;}

    public string c {get;set;}

    }

    我可以通过反射 得到 c 和 A的属性并且能赋值, 但我 如何把B里的A赋值那

    • 已移动 Jie BaoModerator 2011年10月27日 3:46 (发件人:Windows Presentation Foundation)
    2011年10月27日 2:06

答案

  • 非WPF问题,移至C#论坛。

    然后,答案是:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          B b = new B();
          b.a = new A() { Name = "Hello" };
          b.c = "C#";
    
          PropertyInfo propA = typeof(B).GetProperty("a", BindingFlags.Public|BindingFlags.Instance);
          PropertyInfo propC = typeof(B).GetProperty("c", BindingFlags.Public | BindingFlags.Instance);
    
          A aValue = propA.GetValue(b, null) as A;
          Console.WriteLine("B.a property .Name value is: {0}", aValue.Name);
          Console.WriteLine("B.c field value is: {0}", propC.GetValue(b, null));
    
    
          //Set value to property via reflection
          A newa = new A() { Name = "World" };
          propA.SetValue(b, newa, null);
          aValue = propA.GetValue(b, null) as A;
          Console.WriteLine("B.a property .Name value is: {0}", aValue.Name);
    
          //Set a.Name directly
          aValue.Name = "New World"; 
          //Get property A value again
          aValue = propA.GetValue(b, null) as A;
          Console.WriteLine("B.a property .Name value is: {0}", aValue.Name);
    
          Console.ReadLine();
        }
      }
    
    
      public class A
      {
        public string Name { get; set; }
      }
    
      public class B
      {
        public A a { get; set; }
        public string c { get; set; }
      }
    
    }
    
    

    Sincerely,


    Bob Bao [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.

    • 已标记为答案 MakubeXSoft 2011年10月27日 6:23
    2011年10月27日 3:46
    版主
  • 当然能,

    创建一个B对象:

          Assembly asm = Assembly.GetExecutingAssembly();  
          object obj= asm.CreateInstance("B");
    



    Bob Bao [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.

    • 已标记为答案 MakubeXSoft 2011年10月27日 7:08
    2011年10月27日 7:06
    版主

全部回复

  • 非WPF问题,移至C#论坛。

    然后,答案是:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          B b = new B();
          b.a = new A() { Name = "Hello" };
          b.c = "C#";
    
          PropertyInfo propA = typeof(B).GetProperty("a", BindingFlags.Public|BindingFlags.Instance);
          PropertyInfo propC = typeof(B).GetProperty("c", BindingFlags.Public | BindingFlags.Instance);
    
          A aValue = propA.GetValue(b, null) as A;
          Console.WriteLine("B.a property .Name value is: {0}", aValue.Name);
          Console.WriteLine("B.c field value is: {0}", propC.GetValue(b, null));
    
    
          //Set value to property via reflection
          A newa = new A() { Name = "World" };
          propA.SetValue(b, newa, null);
          aValue = propA.GetValue(b, null) as A;
          Console.WriteLine("B.a property .Name value is: {0}", aValue.Name);
    
          //Set a.Name directly
          aValue.Name = "New World"; 
          //Get property A value again
          aValue = propA.GetValue(b, null) as A;
          Console.WriteLine("B.a property .Name value is: {0}", aValue.Name);
    
          Console.ReadLine();
        }
      }
    
    
      public class A
      {
        public string Name { get; set; }
      }
    
      public class B
      {
        public A a { get; set; }
        public string c { get; set; }
      }
    
    }
    
    

    Sincerely,


    Bob Bao [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.

    • 已标记为答案 MakubeXSoft 2011年10月27日 6:23
    2011年10月27日 3:46
    版主
  • 你好.如果现在我甚至都不知道的 类里面具体的类型呢, 不能去new 如何赋值.
    Everyone Is NO.1
    2011年10月27日 6:23
  • 当然能,

    创建一个B对象:

          Assembly asm = Assembly.GetExecutingAssembly();  
          object obj= asm.CreateInstance("B");
    



    Bob Bao [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.

    • 已标记为答案 MakubeXSoft 2011年10月27日 7:08
    2011年10月27日 7:06
    版主
  • 灰常感谢...
    Everyone Is NO.1
    2011年10月27日 7:09
  • CreateInstance 后面需要写 Type的 FullName


    Everyone Is NO.1
    2011年10月27日 7:27