diff options
Diffstat (limited to 'StardewModdingAPI/Extensions.cs')
-rw-r--r-- | StardewModdingAPI/Extensions.cs | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/StardewModdingAPI/Extensions.cs b/StardewModdingAPI/Extensions.cs index c504f470..d4b582b7 100644 --- a/StardewModdingAPI/Extensions.cs +++ b/StardewModdingAPI/Extensions.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using Microsoft.Xna.Framework; @@ -59,6 +60,42 @@ namespace StardewModdingAPI hash ^= v.GetHashCode(); } return hash; - } + } + + public static T Cast<T>(this object o) where T : class + { + return o as T; + } + + public static FieldInfo[] GetPrivateFields(this object o) + { + return o.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic); + } + + public static FieldInfo GetBaseFieldInfo(this Type t, string name) + { + return t.GetField(name, BindingFlags.Instance | BindingFlags.NonPublic); + } + + public static T GetBaseFieldValue<T>(this Type t, object o, string name) where T : class + { + return t.GetBaseFieldInfo(name).GetValue(o) as T; + } + + /* + public static T GetBaseFieldValue<T>(this object o, string name) where T : class + { + return o.GetType().GetBaseFieldInfo(name).GetValue(o) as T; + }*/ + + public static object GetBaseFieldValue(this object o, string name) + { + return o.GetType().GetBaseFieldInfo(name).GetValue(o); + } + + public static void SetBaseFieldValue (this object o, string name, object newValue) + { + o.GetType().GetBaseFieldInfo(name).SetValue(o, newValue); + } } }
\ No newline at end of file |