博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
反射 模板赋值
阅读量:4671 次
发布时间:2019-06-09

本文共 11401 字,大约阅读时间需要 38 分钟。

static void Main(string[] args)        {            string prefix = "td_";            Type type = typeof(TestClass);            PropertyInfo[] propertyinfos = type.GetProperties();            Type type2 = typeof(TestClass2);            PropertyInfo[] propertyinfos2 = type2.GetProperties();            object obj = Activator.CreateInstance(type);            object obj2 = Activator.CreateInstance(type2);            foreach (var p in propertyinfos)            {                object value = p.GetValue(obj, null);                string name = p.Name;                foreach (var p2 in propertyinfos2)                {                    if (p2.Name == prefix + name)                    {                        p2.SetValue(obj2, value, null);                    }                }            }            Show(obj);            Show(obj2);            System.Console.Read();        }
public static void Show(object o)        {            Type type = o.GetType();            PropertyInfo[] myproperties = type.GetProperties();            foreach (var p in myproperties)            {                object value = p.GetValue(o, null);                Console.WriteLine("Name:" + p.Name + "    Value:" + value);            }            Console.WriteLine("----------------------------------");        }

 

public class TestClass    {        public TestClass()        {            Str = "Strss";            Str2 = "Str2ss";            lists.Add(5);            lists.Add(8);        }        public string Str { get; set; }        public string Str2 { get; set; }        public List
lists = new List
(); } public class TestClass2 { public TestClass2() { } public string td_Str { get; set; } public string td_Str2 { get; set; } }

第二阶段

static void Main(string[] args)        {            string prefix = "td_";            object o = Evaluation(typeof(TestClass2), prefix, typeof(TestClass));            Show(o);            System.Console.Read();        }        ///         /// 模板赋值        ///         /// 一般就是数据库里的数据类,带前缀的        /// 前缀        /// 就是提供数据的类        public static object Evaluation(Type BaseClass, string prefix, Type DataClass)        {            PropertyInfo[] propertyinfos = DataClass.GetProperties();            PropertyInfo[] propertyinfos2 = BaseClass.GetProperties();            object obj = Activator.CreateInstance(DataClass);            object obj2 = Activator.CreateInstance(BaseClass);            foreach (var p in propertyinfos)            {                object value = p.GetValue(obj, null);                string name = p.Name;                foreach (var p2 in propertyinfos2)                {                    if (p2.Name == prefix + name)                    {                        p2.SetValue(obj2, value, null);                    }                }            }            return obj2;        }        public static void Show(object o)        {            Type type = o.GetType();            PropertyInfo[] myproperties = type.GetProperties();            foreach (var p in myproperties)            {                object value = p.GetValue(o, null);                Console.WriteLine("Name:" + p.Name + "    Value:" + value);            }            Console.WriteLine("----------------------------------");        }

第三阶段

 

static void Main(string[] args)        {            string prefix = "td_";            TestClass t = new TestClass();            t.Str = "SSSTTTRRR";            t.Str2 = "SSSTTTRRR222";            object o = Evaluation(typeof(TestClass2), prefix, t);            Show(o);            System.Console.Read();        }        ///         /// 模板赋值        ///         /// 一般就是数据库里的数据类,带前缀的        /// 前缀        /// 就是提供数据的类        public static object Evaluation(Type BaseClass, string prefix, object data)        {            Type DataClass = data.GetType();            PropertyInfo[] propertyinfos = DataClass.GetProperties();            PropertyInfo[] propertyinfos2 = BaseClass.GetProperties();            object obj = Activator.CreateInstance(DataClass);            object obj2 = Activator.CreateInstance(BaseClass);            foreach (var p in propertyinfos)            {                object value = p.GetValue(obj, null);                string name = p.Name;                foreach (var p2 in propertyinfos2)                {                    if (p2.Name == prefix + name)                    {                        p2.SetValue(obj2, value, null);                    }                }            }            return obj2;        }

不是想要的,得到的值时类的,不是对象的。

如果能将字符串转换为可执行文本就好了

static void Main(string[] args)        {            string prefix = "td_";            TestClass t = new TestClass();            t.Str = "SSSTTTRRR";            t.Str2 = "SSSTTTRRR222";            object o = Evaluation(typeof(TestClass2), prefix, t);            Show(o);            System.Console.Read();        }        ///         /// 模板赋值        ///         /// 一般就是数据库里的数据类,带前缀的        /// 前缀        /// 就是提供数据的类        public static object Evaluation(Type BaseClass, string prefix, object data)        {            Type DataClass = data.GetType();            PropertyInfo[] propertyinfos = DataClass.GetProperties();            PropertyInfo[] propertyinfos2 = BaseClass.GetProperties();            object obj = Activator.CreateInstance(DataClass);            object obj2 = Activator.CreateInstance(BaseClass);            foreach (var p in propertyinfos)            {                object value = p.GetValue(data, null);                string name = p.Name;                foreach (var p2 in propertyinfos2)                {                    if (p2.Name == prefix + name)                    {                        p2.SetValue(obj2, value, null);                    }                }            }            return obj2;        }

对象对对象进行赋值

static void Main(string[] args)        {            string prefix = "td_";            TestClass t = new TestClass();            t.Str = "SSSTTTRRR";            t.Str2 = "SSSTTTRRR222";            TestClass2 t2 = new TestClass2();            Show(t2);            t2 = Evaluation(t2, prefix, t) as TestClass2;            Show(t2);            System.Console.Read();        }        ///         /// 模板赋值        ///         /// 一般就是数据库里的数据类,带前缀的        /// 前缀        /// 就是提供数据的类        public static object Evaluation(object baseObject, string prefix, object data)        {            Type DataClass = data.GetType();            Type BaseClass = baseObject.GetType();            PropertyInfo[] propertyinfos = DataClass.GetProperties();            PropertyInfo[] propertyinfos2 = BaseClass.GetProperties();            //object obj = Activator.CreateInstance(DataClass);            //object obj2 = Activator.CreateInstance(BaseClass);            foreach (var p in propertyinfos)            {                object value = p.GetValue(data, null);                string name = p.Name;                foreach (var p2 in propertyinfos2)                {                    if (p2.Name == prefix + name)                    {                        p2.SetValue(baseObject, value, null);                    }                }            }            return baseObject;        }

这样就可以动态创建所有的数据实体对象,尤其是还要为每个数据实体对象的属性赋值。

public class TestClass    {        public TestClass()        {            Str = "Strss";            Str2 = "Str2ss";            Num = 10;            Time = DateTime.Now;            lists.Add(5);            lists.Add(8);        }        public string Str { get; set; }        public string Str2 { get; set; }        public int Num { get; set; }        public DateTime Time { get; set; }        public List
lists = new List
(); } public class TestClass2 { public TestClass2() { } public string td_Str { get; set; } public string td_Str2 { get; set; } public int td_Num { get; set; } public DateTime td_Time { get; set; } }

带类型转换的,属性的名称多个前缀,类型不同

class Program    {        static void Main(string[] args)        {            string prefix = "td_";            TestClass t = new TestClass();            t.Str = "SSSTTTRRR";            t.Str2 = "SSSTTTRRR222";            t.Num = 55;            t.Time = DateTime.Now;            TestClass2 t2 = new TestClass2();            Show(t2);            t2 = Evaluation(t2, prefix, t) as TestClass2;            Show(t2);            System.Console.Read();        }        ///         /// 模板赋值        ///         /// 一般就是数据库里的数据类,带前缀的        /// 前缀        /// 就是提供数据的类        public static object Evaluation(object baseObject, string prefix, object data)        {            Type DataClass = data.GetType();            Type BaseClass = baseObject.GetType();            PropertyInfo[] propertyinfos = DataClass.GetProperties();            PropertyInfo[] propertyinfos2 = BaseClass.GetProperties();            foreach (var p in propertyinfos)            {                object value = p.GetValue(data, null);                string name = p.Name;                                 foreach (var p2 in propertyinfos2)                {                    if (p2.Name == prefix + name)                    {                        string ty = p2.PropertyType.Name.ToString();                        if (ty == "String")                            value = value.ToString();                        else if (ty == "Int32")                            value = Convert.ToInt32(value);                        else if (ty == "Single")                            value = Convert.ToSingle(value);                        else if (ty == "Double")                            value = Convert.ToDouble(value);                        else if (ty == "DateTime")                            value = Convert.ToDateTime(value);                        else if (ty == "Decimal")                            value = Convert.ToDecimal(value);                        p2.SetValue(baseObject, value, null);                    }                }            }            return baseObject;        }        public static void Show(object o)        {            Type type = o.GetType();            PropertyInfo[] myproperties = type.GetProperties();            foreach (var p in myproperties)            {                object value = p.GetValue(o, null);                string cla = p.PropertyType.Name.ToString();                Console.WriteLine("Name:" + p.Name + "    Value:" + value+"   Attributes:"+cla);            }            Console.WriteLine("----------------------------------");        }    }    public class TestClass    {        public TestClass()        {            Str = "Strss";            Str2 = "Str2ss";            Num = 10;            Time = DateTime.Now;            lists.Add(5);            lists.Add(8);        }        public string Str { get; set; }        public string Str2 { get; set; }        public int Num { get; set; }        public DateTime Time { get; set; }        public List
lists = new List
(); } public class TestClass2 { public TestClass2() { } public int num; public DateTime dt; public string td_Str { get; set; } public string td_Str2 { get; set; } public string td_Num { get; set; } public string td_Time { get; set; } }

 

 

转载于:https://www.cnblogs.com/hongdada/archive/2013/03/21/2973536.html

你可能感兴趣的文章
PADS无模命令总结
查看>>
潭州课堂25班:Ph201805201 爬虫高级 第十二 课 Scrapy-redis分布 项目实战 (课堂笔记)...
查看>>
潭州课堂25班:Ph201805201 django 项目 第二课 git 版本控制 (课堂笔记)
查看>>
PKU 1012
查看>>
【nodejs】让nodejs像后端mvc框架(asp.net mvc)一orm篇【如EF般丝滑】typeorm介绍(8/8)...
查看>>
【nodejs】让nodejs像后端mvc框架(asp.net mvc)一样处理请求--请求处理函数装饰器注册篇(5/8)【controller+action】...
查看>>
[转]Java8 Lambda表达式教程
查看>>
[MySQL 5.6] MySQL 5.6 group commit 性能测试及内部实现流程
查看>>
定时器与锁
查看>>
Tomcat的部署+第一个Servlet
查看>>
javaweb中解决中文乱码问题
查看>>
3-8《Ruby元编程》第二章对象模型
查看>>
987. Binary Number with Alternating Bits
查看>>
十四、Hadoop学习笔记————Zookeeper概述与基本概念
查看>>
回调函数的原理及PHP实例
查看>>
卸载驱动出现:rmmod: can't change directory to '/lib/modules': No such file or directory
查看>>
Scratch与物理·天文:模拟中国嫦娥探月工程,探索月球的背面!
查看>>
大话数据结构 -04-3 队列
查看>>
插入排序算法(C实现)
查看>>
多线程技术 初步
查看>>