反射动态创建对象_Json为例
1. 取得數據類型Type
方式一:Type.GetType(“類型全名”);
?????????????????? 適合于類型的名稱已知
方式二:obj.GetType();
?????????????????? 適合于類型名未知,類型未知,存在已有對象
方式三:typeof(類型)
?????????????????? 適合于已知類型
方式四:Assembly.Load(“XXX”).GetType(“名字”);
???????? 適合于類型在另一個程序集中
?
實例
Type type =Type.GetType("Day07.MainCity");
???????????//Type type = city.GetType();
???????????//Type type = typeof(MainCity);
2. 動態創建對象
Activator.CreateInstance(string 程序集名稱,string 類型全名)
?Activator.CreateInstance(Type type);
?
Assembly assembly = Assembly.Load(程序集);
assembly.CreateInstance(Type);
?
//找到有參構造方法,動態調用構造方法
type.GetConstructor(typeof(string)).Invoke()
?
實例
//動態創建對象
???????????object obj = Activator.CreateInstance(type);
?
3.Type類常用Get系列方法 Is系列屬性。
2.????? MethodInfo(方法)
重要方法: Invoke
?
MethodInfo method = type.GetMethod("PrintName");
method.Invoke(obj, null);
???????????
MethodInfo methodFun1 =type.GetMethod("Fun1", BindingFlags.NonPublic |BindingFlags.Instance);
methodFun1.Invoke(obj, null);?
?
3.????? PropertyInfo(屬性)
???????? 重要方法:SetValueGetValue
4.????? FieldInfo(字段)
???????? 重要方法:SetValueGetValue
5.????? ConstructInfo(構造方法)
???????? 重要方法:Invoke
?
?
?
?
?
實例
//獲取類型信息
???????????PropertyInfo property = type.GetProperty("Name");
???????????property.SetValue(obj, "abc");
???????????//property.SetValue(obj, "100");
?
?
?
?
屬性類型轉換
?
?
//1.屬性類型:
property.PropertyType?
//2.字符串轉換為屬性類型:
???????????//Convert.ChangeType("100", property.PropertyType);
?
?
?
?
?
?
?
?
?
?
?
?
?
??????/*
????????Json 字符串格式
????????*? {"Name" : "abc","HP":"100"}???????????
????????*?
????????JsonHelper
????????-- c# 對象 -->? Json字符串
?
?
????????-- Json字符串 -->? c#對象
????????* (提示
????????*??? 1. 泛型方法
????????*??? 2.
????????* )
????????
????????*/
?
?
?
?
?
?
反射與緩存結合使用
?
?
?
?
?
轉換方法實現
????????//-- c# 對象 -->? Json字符串
???????//{"Name" : "abc","HP":"100"}???????????
???????public static string ObjectToString(object obj)
???????{
???????????StringBuilder builder = new StringBuilder();
???????????builder.Append("{");
???????????//獲取類型信息
???????????Type type = obj.GetType();
???????????//獲取當前類型所有屬性
???????????foreach (var p in type.GetProperties())
???????????{
???????????????builder.AppendFormat("\"{0}\":\"{1}\",",p.Name, p.GetValue(obj));
???????????}
???????????builder.Remove(builder.Length- 1, 1);
???????????builder.Append("}");
???????????return builder.ToString();
???????}
????????
????????//-- Json字符串 -->? c#對象
????????//{"Name" : "abc","HP":"100"}?
???????public static T StringToObject<T>(string jsonString)//whereT:new()
???????{
???????????//創建對象
???????????//T obj = new T();
???????????Type type = typeof(T);
???????????object obj = Activator.CreateInstance(type);
???????????//根據字符串為屬性賦值
???????????//{"Name" : "abc","HP":"100"}?
???????????//去除字符 {?????? "????? }
???????????jsonString = jsonString.Replace("{", "").Replace("\"","").Replace("}", "");
???????????//Name:abc,HP:100,MaxHP:100
???????????string[] keyValue = jsonString.Split(',', ':');
???????????for (int i = 0; i < keyValue.Length; i+=2)
???????????{
????????????????? //keyValue[i]?? keyValue[i+1]
???????????????var property =type.GetProperty(keyValue[i]);
??????????????? object propertyVal =Convert.ChangeType(keyValue[i + 1], property.PropertyType);
??????????????? property.SetValue(obj,propertyVal);
???????????}
???????????return (T)obj;
???????}
??? }
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的反射动态创建对象_Json为例的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Unity自定义事件相应区域
 - 下一篇: C#中利用反射循环给一些字段赋值