////////////////////////////////////////////////// public Object GetValueOrMethod(Object Item, String PropertyName, Int32 ParametersCount, out Boolean FoundValue) { FoundValue = false; var getter = GetGetter(Item, PropertyName); if (getter != null) { FoundValue = true; try { return(getter(Item)); } catch { return(MyReflectionHelper.GetValue( Item, PropertyName)); } } else { var method = MyReflectionHelper.GetMethod(Item, PropertyName, ParametersCount); if (method != null) { FoundValue = true; } return(method); } return(null); }
//////////////////////////////////////// public static DynamicCallResult CallMethod( #if !NET20 this #endif Object Obj, String MethodName, IList <Object> Parameters) { MethodInfo methodInfo = MyReflectionHelper.GetMethod( Obj, MethodName, Parameters == null ? 0 : Parameters.Count); if (methodInfo != null) { IList <ParameterInfo> parametersInfo = methodInfo.GetParameters(); List <Object> finalParameters = new List <Object>(); if (parametersInfo.Count == 1 && parametersInfo[0].ParameterType == typeof(DynLanMethodParameters)) { foreach (Object parameter in Parameters) { finalParameters.Add(parameter); } finalParameters = new List <object>() { new DynLanMethodParameters() { Parameters = finalParameters.ToArray() } }; } else { Int32 index = -1; foreach (ParameterInfo parameterInfo in parametersInfo) { index++; Object val = MyTypeHelper.ConvertTo( Parameters[index], parameterInfo.ParameterType); finalParameters.Add(val); } } return(new DynamicCallResult() { Value = Obj is Type? methodInfo.Invoke(null, finalParameters.ToArray()) : methodInfo.Invoke(Obj, finalParameters.ToArray()) }); } return(null); }
public Object GetValue(Object Item, String PropertyName) { var getter = GetGetter(Item, PropertyName); if (getter != null) { try { return(getter(Item)); } catch { return(MyReflectionHelper.GetValue( Item, PropertyName)); } } return(null); }
public DataType GetValue <DataType>(Object Item, String PropertyName) { var getter = GetGetter(Item, PropertyName); if (getter != null) { try { return((DataType)getter(Item)); } catch { return((DataType)MyReflectionHelper.GetValue( Item, PropertyName)); } } return(default(DataType)); }
public bool SetValue <DataType>(Object Item, String PropertyName, DataType Value) { MemberSetter setter = GetSetter(Item, PropertyName); if (setter != null) { try { setter(Item, Value); } catch { Type t1 = GetPropertyType(Item, PropertyName); // Item.GetType().GetProperty(PropertyName); Type t2 = Value == null ? null : Value.GetType(); // typeof(DataType); if (t2 == null || t1.Equals(t2)) { MyReflectionHelper.SetValue(Item, PropertyName, Value); } else { Object newValue = MyTypeHelper.ConvertTo(Value, t1); try { setter(Item, newValue); } catch { MyReflectionHelper.SetValue(Item, PropertyName, newValue); } } throw; } return(true); } else { } return(false); }