反射 中 一个A类的字段包括另一个B类的对象,如何通过A类获取B类中的字段

zhufei__ 2017-07-14 04:30:27
public class test01
{
public string A1;
public string A2;
public tset02 t2;
}

public class tset02
{
public string t1;
public string t2;
}


如何在获取test01字段的时候,也可以获取到test02的字段
...全文
220 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
在增加一个事件的反射,来试试
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = new test01
            {
                A1 = "aaa",
                A2 = "bbbb",
                t2 = new tset02
                {
                    t1 = "1111",
                    t2 = "2222"
                }
            };
            Test(x);
            Console.WriteLine("...............按任意键退出");
            Console.ReadKey();
        }

        private static void Test(dynamic x)
        {
            Console.WriteLine("A1 = {0}", x.A1);
            Console.WriteLine("t1 = {0}", x.t2.t1);
            x.t2.lenChanged += (Action<int>)(value =>
            {
                Console.WriteLine("len changed -> {0}", value);
            });
            x.t2.t2 = "123456789";
            Console.WriteLine("t2.len = {0}", x.t2.len());
        }
    }

    public class test01
    {
        public string A1;
        public string A2;
        public tset02 t2;
    }

    public class tset02
    {
        public string t1;

        private string _t2;

        public string t2
        {
            get
            {
                return _t2;
            }
            set
            {
                _t2 = value;
                if (lenChanged != null)
                    lenChanged(len());
            }
        }

        public int len()
        {
            return t1.Length + t2.Length;
        }

        public event Action<int> lenChanged;
    }


}
dynamic 是很多年前的 .net 框架扩展了。 在软件设计中,其实并不应该滥用反射、滥用动态代码、滥用横切方式。但是假设你不了解 c# 的高级写法,那么反而容易滥用概念。
  • 打赏
  • 举报
回复
稍微多写两行,例如
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = new test01
            {
                A1 = "aaa",
                A2 = "bbbb",
                t2 = new tset02
                {
                    t1 = "1111",
                    t3 = "2222"
                }
            };
            Test(x);
            Console.WriteLine("...............按任意键退出");
            Console.ReadKey();
        }

        private static void Test(dynamic x)
        {
            Console.WriteLine("A1 = {0}", x.A1);
            Console.WriteLine("t1 = {0}", x.t2.t1);
            x.t2.t3 = "123456789";
            Console.WriteLine("t2.len = {0}", x.t2.len());
        }
    }

    public class test01
    {
        public string A1;
        public string A2;
        public tset02 t2;
    }

    public class tset02
    {
        public string t1;
        public string t3;

        public int len()
        {
            return t1.Length + t3.Length;
        }
    }


}
你如果不知道 dynamic 技术,那么查查 c# 语法文档,看看自己从哪一年的 c# 技术落后了啊?!
  • 打赏
  • 举报
回复
给你写一个例子:
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var x = new test01
            {
                A1 = "aaa",
                A2 = "bbbb",
                t2 = new tset02
                {
                    t1 = "1111",
                    t3 = "2222"
                }
            };
            Test(x);
            Console.WriteLine("...............按任意键退出");
            Console.ReadKey();
        }

        private static void Test(dynamic x)
        {
            Console.WriteLine("A1 = {0}", x.A1);
            Console.WriteLine("t1 = {0}", x.t2.t1);
        }
    }

    public class test01
    {
        public string A1;
        public string A2;
        public tset02 t2;
    }

    public class tset02
    {
        public string t1;
        public string t3;
    }


}
用什么“反射”语法?你要学习最近10年的c#技术,并不用写成那种反射代码!
zhufei__ 2017-07-14
  • 打赏
  • 举报
回复
还有就是,如果想给test02 的t1,t2赋值的话,应该怎样操作呢
zhufei__ 2017-07-14
  • 打赏
  • 举报
回复
引用 1 楼 ducker3590 的回复:

Type nestedType = type.GetNestedType("t2");
然后按照正常反射一个类操作就行
type.GetNestedType("t2"); // 这个里面的参数不管写t2还是test02,都是null,获取不到
csdnFUCKINGSUCKS 2017-07-14
  • 打赏
  • 举报
回复

Type nestedType = type.GetNestedType("t2");
然后按照正常反射一个类操作就行

110,524

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧