Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, it is:</p> <pre><code> /// &lt;summary&gt; /// Returns a _private_ Property Value from a given Object. Uses Reflection. /// Throws a ArgumentOutOfRangeException if the Property is not found. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;Type of the Property&lt;/typeparam&gt; /// &lt;param name="obj"&gt;Object from where the Property Value is returned&lt;/param&gt; /// &lt;param name="propName"&gt;Propertyname as string.&lt;/param&gt; /// &lt;returns&gt;PropertyValue&lt;/returns&gt; public static T GetPrivatePropertyValue&lt;T&gt;(this object obj, string propName) { if (obj == null) throw new ArgumentNullException("obj"); PropertyInfo pi = obj.GetType().GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); if (pi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName)); return (T)pi.GetValue(obj, null); } /// &lt;summary&gt; /// Returns a private Property Value from a given Object. Uses Reflection. /// Throws a ArgumentOutOfRangeException if the Property is not found. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;Type of the Property&lt;/typeparam&gt; /// &lt;param name="obj"&gt;Object from where the Property Value is returned&lt;/param&gt; /// &lt;param name="propName"&gt;Propertyname as string.&lt;/param&gt; /// &lt;returns&gt;PropertyValue&lt;/returns&gt; public static T GetPrivateFieldValue&lt;T&gt;(this object obj, string propName) { if (obj == null) throw new ArgumentNullException("obj"); Type t = obj.GetType(); FieldInfo fi = null; while (fi == null &amp;&amp; t != null) { fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); t = t.BaseType; } if (fi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Field {0} was not found in Type {1}", propName, obj.GetType().FullName)); return (T)fi.GetValue(obj); } /// &lt;summary&gt; /// Sets a _private_ Property Value from a given Object. Uses Reflection. /// Throws a ArgumentOutOfRangeException if the Property is not found. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;Type of the Property&lt;/typeparam&gt; /// &lt;param name="obj"&gt;Object from where the Property Value is set&lt;/param&gt; /// &lt;param name="propName"&gt;Propertyname as string.&lt;/param&gt; /// &lt;param name="val"&gt;Value to set.&lt;/param&gt; /// &lt;returns&gt;PropertyValue&lt;/returns&gt; public static void SetPrivatePropertyValue&lt;T&gt;(this object obj, string propName, T val) { Type t = obj.GetType(); if (t.GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) == null) throw new ArgumentOutOfRangeException("propName", string.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName)); t.InvokeMember(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, obj, new object[] { val }); } /// &lt;summary&gt; /// Set a private Property Value on a given Object. Uses Reflection. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;Type of the Property&lt;/typeparam&gt; /// &lt;param name="obj"&gt;Object from where the Property Value is returned&lt;/param&gt; /// &lt;param name="propName"&gt;Propertyname as string.&lt;/param&gt; /// &lt;param name="val"&gt;the value to set&lt;/param&gt; /// &lt;exception cref="ArgumentOutOfRangeException"&gt;if the Property is not found&lt;/exception&gt; public static void SetPrivateFieldValue&lt;T&gt;(this object obj, string propName, T val) { if (obj == null) throw new ArgumentNullException("obj"); Type t = obj.GetType(); FieldInfo fi = null; while (fi == null &amp;&amp; t != null) { fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); t = t.BaseType; } if (fi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Field {0} was not found in Type {1}", propName, obj.GetType().FullName)); fi.SetValue(obj, val); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload