public static void SetStaticPublicField(string typeName, string name, object value) { BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy; Type type = Type.GetType(typeName); FieldInfo fieldInfo = PublicExtensions.GetFieldInfo(type, name, flags); if (fieldInfo != null) { if (fieldInfo.FieldType == typeof(int)) { int num = Convert.ToInt32(value); fieldInfo.SetValue(null, num); return; } if (fieldInfo.FieldType == typeof(float)) { float num2 = Convert.ToSingle(value); fieldInfo.SetValue(null, num2); return; } if (fieldInfo.FieldType == typeof(long)) { long num3 = Convert.ToInt64(value); fieldInfo.SetValue(null, num3); return; } fieldInfo.SetValue(null, value); } }
public static void SetPrivateField(this object obj, string name, object value) { BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy; Type type = obj.GetType(); FieldInfo fieldInfo = PublicExtensions.GetFieldInfo(type, name, flags); if (fieldInfo != null) { if (fieldInfo.FieldType == typeof(int)) { int num = Convert.ToInt32(value); fieldInfo.SetValue(obj, num); return; } if (fieldInfo.FieldType == typeof(float)) { float num2 = Convert.ToSingle(value); fieldInfo.SetValue(obj, num2); return; } if (fieldInfo.FieldType == typeof(long)) { long num3 = Convert.ToInt64(value); fieldInfo.SetValue(obj, num3); return; } fieldInfo.SetValue(obj, value); } }
static int GetFieldInfo(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 3); Type arg0 = LuaScriptMgr.GetTypeObject(L, 1); string arg1 = LuaScriptMgr.GetLuaString(L, 2); System.Reflection.BindingFlags arg2 = (System.Reflection.BindingFlags)LuaScriptMgr.GetNetObject(L, 3, typeof(System.Reflection.BindingFlags)); System.Reflection.FieldInfo o = PublicExtensions.GetFieldInfo(arg0, arg1, arg2); LuaScriptMgr.PushObject(L, o); return(1); }
private static int GetFieldInfo(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 3); Type typeObject = LuaScriptMgr.GetTypeObject(L, 1); string luaString = LuaScriptMgr.GetLuaString(L, 2); BindingFlags flags = (BindingFlags)((int)LuaScriptMgr.GetNetObject(L, 3, typeof(BindingFlags))); FieldInfo fieldInfo = PublicExtensions.GetFieldInfo(typeObject, luaString, flags); LuaScriptMgr.PushObject(L, fieldInfo); return(1); }
public static object GetStaticPublicField(string typeName, string name) { BindingFlags flags = BindingFlags.Static | BindingFlags.Public; Type type = Type.GetType(typeName); FieldInfo fieldInfo = PublicExtensions.GetFieldInfo(type, name, flags); if (fieldInfo != null) { return(fieldInfo.GetValue(null)); } return(null); }
public static object GetPublicField(this object obj, string name) { BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy; Type type = obj.GetType(); FieldInfo fieldInfo = PublicExtensions.GetFieldInfo(type, name, flags); if (fieldInfo != null) { return(fieldInfo.GetValue(obj)); } return(null); }
public static T GetPrivateFieldGeneric <T>(this object obj, string name) { BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy; Type type = obj.GetType(); FieldInfo fieldInfo = PublicExtensions.GetFieldInfo(type, name, flags); if (fieldInfo != null) { return((T)((object)fieldInfo.GetValue(obj))); } return(default(T)); }
public static FieldInfo GetFieldInfo(Type type, string name, BindingFlags flags) { if (type == null) { return(null); } FieldInfo field = type.GetField(name, flags); if (field == null && type.BaseType != null) { return(PublicExtensions.GetFieldInfo(type.BaseType, name, flags)); } return(field); }