diff --git a/SkinHide/Utils/RefHelp.cs b/SkinHide/Utils/RefHelp.cs index 1b5a9f4..8c60a04 100644 --- a/SkinHide/Utils/RefHelp.cs +++ b/SkinHide/Utils/RefHelp.cs @@ -149,7 +149,7 @@ namespace SkinHide.Utils } } - public PropertyRef(PropertyInfo propertyinfo, object instance = null) + public PropertyRef(PropertyInfo propertyinfo, object instance) { if (propertyinfo == null) { @@ -159,9 +159,11 @@ namespace SkinHide.Utils Init(propertyinfo, instance); } - public PropertyRef(Type type, string propertyname, object instance = null) + public PropertyRef(Type type, string propertyname, bool declaredonly, object instance) { - PropertyInfo propertyInfo = type.GetProperty(propertyname, AccessTools.all); + BindingFlags flags = declaredonly ? AccessTools.allDeclared : AccessTools.all; + + PropertyInfo propertyInfo = type.GetProperty(propertyname, flags); if (propertyInfo == null) { @@ -171,9 +173,11 @@ namespace SkinHide.Utils Init(propertyInfo, instance); } - public PropertyRef(Type type, string[] propertynames, object instance = null) + public PropertyRef(Type type, string[] propertynames, bool declaredonly, object instance) { - PropertyInfo propertyInfo = propertynames.Select(x => type.GetProperty(x, AccessTools.all)).FirstOrDefault(x => x != null); + BindingFlags flags = declaredonly ? AccessTools.allDeclared : AccessTools.all; + + PropertyInfo propertyInfo = propertyInfo = propertynames.Select(x => type.GetProperty(x, flags)).FirstOrDefault(x => x != null); if (propertyInfo == null) { @@ -206,29 +210,29 @@ namespace SkinHide.Utils } } - public static PropertyRef Create(PropertyInfo propertyinfo, object instance = null) + public static PropertyRef Create(PropertyInfo propertyinfo, object instance) { return new PropertyRef(propertyinfo, instance); } - public static PropertyRef Create(string propertyname, object instance = null) + public static PropertyRef Create(string propertyname, bool declaredonly = false, object instance = null) { - return new PropertyRef(typeof(T), propertyname, instance); + return new PropertyRef(typeof(T), propertyname, declaredonly, instance); } - public static PropertyRef Create(string[] propertynames, object instance = null) + public static PropertyRef Create(string[] propertynames, bool declaredonly = false, object instance = null) { - return new PropertyRef(typeof(T), propertynames, instance); + return new PropertyRef(typeof(T), propertynames, declaredonly, instance); } - public static PropertyRef Create(Type type, string propertyname, object instance = null) + public static PropertyRef Create(Type type, string propertyname, bool declaredonly = false, object instance = null) { - return new PropertyRef(type, propertyname, instance); + return new PropertyRef(type, propertyname, declaredonly, instance); } - public static PropertyRef Create(Type type, string[] propertynames, object instance = null) + public static PropertyRef Create(Type type, string[] propertynames, bool declaredonly = false, object instance = null) { - return new PropertyRef(type, propertynames, instance); + return new PropertyRef(type, propertynames, declaredonly, instance); } public F GetValue(T instance) @@ -296,7 +300,7 @@ namespace SkinHide.Utils } } - public FieldRef(FieldInfo fieldinfo, object instance = null) + public FieldRef(FieldInfo fieldinfo, object instance) { if (fieldinfo == null) { @@ -306,21 +310,25 @@ namespace SkinHide.Utils Init(fieldinfo, instance); } - public FieldRef(Type type, string fieldname, object instance = null) + public FieldRef(Type type, string fieldname, bool declaredonly, object instance) { - FieldInfo fieldInfo = type.GetField(fieldname, AccessTools.all); + BindingFlags flags = declaredonly ? AccessTools.allDeclared : AccessTools.all; + + FieldInfo fieldInfo = type.GetField(fieldname, flags); if (fieldInfo == null) { - throw new Exception(fieldInfo + " is null"); + throw new Exception(fieldname + " is null"); } Init(fieldInfo, instance); } - public FieldRef(Type type, string[] fieldnames, object instance = null) + public FieldRef(Type type, string[] fieldnames, bool declaredonly, object instance) { - FieldInfo fieldInfo = fieldnames.Select(x => type.GetField(x, AccessTools.all)).FirstOrDefault(x => x != null); + BindingFlags flags = declaredonly ? AccessTools.allDeclared : AccessTools.all; + + FieldInfo fieldInfo = fieldnames.Select(x => type.GetField(x, flags)).FirstOrDefault(x => x != null); if (fieldInfo == null) { @@ -335,24 +343,24 @@ namespace SkinHide.Utils return new FieldRef(fieldinfo, instance); } - public static FieldRef Create(string fieldname, object instance = null) + public static FieldRef Create(string fieldname, bool declaredonly = false, object instance = null) { - return new FieldRef(typeof(T), fieldname, instance); + return new FieldRef(typeof(T), fieldname, declaredonly, instance); } - public static FieldRef Create(string[] fieldnames, object instance = null) + public static FieldRef Create(string[] fieldnames, bool declaredonly = false, object instance = null) { - return new FieldRef(typeof(T), fieldnames, instance); + return new FieldRef(typeof(T), fieldnames, declaredonly, instance); } - public static FieldRef Create(Type type, string fieldname, object instance = null) + public static FieldRef Create(Type type, string fieldname, bool declaredonly = false, object instance = null) { - return new FieldRef(type, fieldname, instance); + return new FieldRef(type, fieldname, declaredonly, instance); } - public static FieldRef Create(Type type, string[] fieldnames, object instance = null) + public static FieldRef Create(Type type, string[] fieldnames, bool declaredonly = false, object instance = null) { - return new FieldRef(type, fieldnames, instance); + return new FieldRef(type, fieldnames, declaredonly, instance); } private void Init(FieldInfo fieldinfo, object instance = null)