C# 反射 和 設定資料
阿新 • • 發佈:2018-12-13
object obj = Activator.CreateInstance(msg_type);
//setvalue
//type
//GetType
protected void DisplayObject(object obj, Type type, string prefix, object parent, System.Reflection.PropertyInfo propertyInfo) { if (obj == null) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(prefix, "null"); inputValue = EditorGUILayout.TextField(inputValue); if (GUILayout.Button("Set Value")) { if (parent.GetType().IsGenericType) { propertyInfo.SetValue(parent, Convert.ChangeType(inputValue, propertyInfo.PropertyType), new object[] { Convert.ToInt32(prefix) }); } else { propertyInfo.SetValue(parent, System.Text.Encoding.Default.GetBytes(inputValue), null); } } EditorGUILayout.EndHorizontal(); } else if (type.IsGenericType) { if (type.GetGenericTypeDefinition() == typeof(List<>)) { var item_type = type.GetGenericArguments()[0]; if (DisplayFoldOut(obj, string.Format("{0} - List<{1}>", prefix, item_type.Name))) { EditorGUI.indentLevel++; IList list = obj as IList; EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Add Item")) { object item_obj = Activator.CreateInstance(item_type); list.Add(item_obj); } EditorGUILayout.EndHorizontal(); for (var i = 0; i < list.Count; ++i) { DisplayObject(list[i], item_type, i.ToString(), list, type.GetProperty("Item")); } EditorGUI.indentLevel--; } } else if (type.GetGenericTypeDefinition() == typeof(Dictionary<,>)) { var generic_args_type = type.GetGenericArguments(); var key_type = generic_args_type[0]; var value_type = generic_args_type[1]; if (DisplayFoldOut(obj, string.Format("{0} - Dictionary<{1}, {2}>", prefix, key_type.Name, value_type.Name))) { EditorGUI.indentLevel++; var dict = obj as IDictionary; foreach (var key in dict.Keys) { var value = dict[key]; DisplayObject(value, value_type, key.ToString(), null, null); } EditorGUI.indentLevel--; } } else { EditorGUILayout.LabelField(prefix, string.Format("invalid generic type : {0}", type.ToString())); } } else if (type.IsValueType) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(prefix, obj.ToString()); inputValue = EditorGUILayout.TextField(inputValue); if (GUILayout.Button("Set Value")) { if (parent.GetType().IsGenericType) { propertyInfo.SetValue(parent, Convert.ChangeType(inputValue, propertyInfo.PropertyType), new object[] { Convert.ToInt32(prefix) }); } else { propertyInfo.SetValue(parent, Convert.ChangeType(inputValue, propertyInfo.PropertyType), null); } } EditorGUILayout.EndHorizontal(); } else if (type == typeof(byte[])) { var text = System.Text.Encoding.UTF8.GetString(obj as byte[]); EditorGUILayout.LabelField(prefix, text); } else if (type.IsClass) { if (DisplayFoldOut(obj, string.Format("{0} - {1}", prefix, type.Name))) { EditorGUI.indentLevel++; foreach (var property in type.GetProperties()) { if (property.CanRead) { var value = property.GetGetMethod().Invoke(obj, null); DisplayObject(value, property.PropertyType, property.Name, obj, property); } } EditorGUI.indentLevel--; } } else { EditorGUILayout.LabelField(prefix, string.Format("invalid type : {0}", type.ToString())); } }