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; } }