public static void SetValueByKey(this object obj, string name, object value) { var property = obj.GetType() .GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (property != null) { FastInvoke.GetMethodInvoker(property.GetSetMethod(true))(obj, new[] { value }); } }
public static object InvokeMethod(this object obj, string method, object[] args) { var mi = obj.GetMethodInfo(method, args); if (mi == null) { throw new NotSupportedException(); } var fastInvoker = FastInvoke.GetMethodInvoker(mi); return(fastInvoker(obj, args)); }
public static object InvokeStaticMethod(this Type type, string method, object[] args) { var mi = type.GetMethodInfo(method, args, true); if (mi == null) { throw new NotSupportedException(); } var fastInvoker = FastInvoke.GetMethodInvoker(mi); return(fastInvoker(null, args)); }
public static object GetValueByKey(this object obj, string name) { object objValue = null; var property = obj.GetType() .GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (property != null) { objValue = FastInvoke.GetMethodInvoker(property.GetGetMethod(true))(obj, null); } return(objValue); }
public static T GetValueByKey <T>(this object obj, string name) { T retValue = default(T); object objValue = null; try { if (obj is Newtonsoft.Json.Linq.JObject) { var jObject = obj as Newtonsoft.Json.Linq.JObject; var property = jObject.Property(name); if (property != null) { var value = property.Value as Newtonsoft.Json.Linq.JValue; if (value != null) { objValue = value.Value; } } } else { var property = obj.GetType().GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (property != null) { objValue = FastInvoke.GetMethodInvoker(property.GetGetMethod(true))(obj, null); //Func<T> PGet = Delegate.CreateDelegate(typeof(Func<T>), obj, property.GetGetMethod(true)) as Func<T>; //objValue = PGet(); // property.GetValue(obj, null); } } if (objValue != null) { retValue = (T)objValue; } } catch (System.Exception) { retValue = default(T); } return(retValue); }