obj -> property
obj.gettype().getproperty
property->obj
info.setvalue(obj)
info.GetGetMethod().Invoke(obj, null);
先拿属性 然后调取他的get{} 方法
//对某个类进行实例化
object obj = Activator.CreateInstance(msg_type);
typeof(classA) == a.GetType()
Type type = a.GetType()
IsGenericType
泛型来说Typeof(List<ClassA>) Dictionary等
可以使用Type.IsGenericType 来判断是否是泛型类型
Convert.ChangeType
转换类型
SetValue
设置数据
propertyInfo.SetValue(parent, Convert.ChangeType(inputValue, propertyInfo.PropertyType), new object[] { Convert.ToInt32(prefix) });
设置list里面的某个数据,propertyInfo为 item的property
设置的时候parent是获取property的obj
type.GetGenericTypeDefinition() == typeof(List<>)
var item_type = type.GetGenericArguments()[0];
type.GetGenericTypeDefinition() == typeof(Dictionary<,>)
var generic_args_type = type.GetGenericArguments();
var key_type = generic_args_type[0];
var value_type = generic_args_type[1];
type.IsValueType
是不是int 等value
IsClass
是否是类
type == typeof(byte[])
是否是byte[]
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--;
}
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()));
}
}