public void SetValue <T, U>(ref T obj, U member) { //结构装箱,不然SetValue自动装箱的值拿不到 object o = obj; if (Member is FieldInfo) { var field = Member as FieldInfo; if (typeof(U) != field.FieldType) { object ov = OeipAttributeHelper.ChangeType(member, field.FieldType); field.SetValue(o, ov); } else { field.SetValue(o, member); } } else if (Member is PropertyInfo) { var property = Member as PropertyInfo; if (typeof(U) != property.PropertyType) { object ov = OeipAttributeHelper.ChangeType(member, property.PropertyType); property.SetValue(o, ov); } else { property.SetValue(o, member); } } obj = (T)o; }
public T GetValue <T>(ref object obj) { T t = default(T); if (Member is FieldInfo) { var field = Member as FieldInfo; var tv = field.GetValue(obj); if (typeof(T) == field.FieldType) { t = (T)tv; } else { t = (T)OeipAttributeHelper.ChangeType(tv, typeof(T)); } } else if (Member is PropertyInfo) { var property = Member as PropertyInfo; var tv = property.GetValue(obj); if (typeof(T) == property.PropertyType) { t = (T)tv; } else { t = (T)OeipAttributeHelper.ChangeType(tv, typeof(T)); } } return(t); }
/// <summary> /// 绑定一个结构/类到一个Panel上面 /// </summary> /// <typeparam name="F"></typeparam> /// <param name="t"></param> /// <param name="panel"></param> /// <param name="onTemplate"></param> /// <param name="onAddPanel"></param> /// <param name="action"></param> public void Bind <P>(T t, P panel, Action <T> action = null) { obj = t; attributes = OeipAttributeHelper.GetAttributes(t); onAction = action; components = new List <IOeipComponent>(); foreach (var attribute in attributes) { IOeipComponent component = CreateComponent(attribute); if (component == null) { continue; } bool bAdd = OnAddPanel(component, panel); if (!bAdd) { continue; } if (component is IOeipComponent <int> ) { SetComponent <IOeipComponent <int>, int>(component as IOeipComponent <int>, attribute); } if (component is IOeipComponent <float> ) { SetComponent <IOeipComponent <float>, float>(component as IOeipComponent <float>, attribute); } if (component is IOeipComponent <string> ) { SetComponent <IOeipComponent <string>, string>(component as IOeipComponent <string>, attribute); } if (component is IOeipComponent <bool> ) { SetComponent <IOeipComponent <bool>, bool>(component as IOeipComponent <bool>, attribute); } components.Add(component); } //更新所有控件值的显示 Update(); //让子类在这里也可以做些事 OnBind(); }