Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET Get private property via Reflection
    text
    copied!<p>I've the following scenario</p> <p>Assebly A</p> <pre><code>public abstract class MyBaseEntity { //Uncompleted method public void addChild&lt;T&gt;(T child) { try { Type tInfo = this.GetType(); PropertyInfo pInfo = tInfo.GetProperties(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance).Where(p =&gt; p.PropertyType == typeof(ISet&lt;T&gt;)).FirstOrDefault(); MethodInfo mInfo = tInfo.GetMethod("Add"); mInfo.Invoke(this, new object[] {child}); } catch (Exception ex) { throw ex; } } } </code></pre> <p>Assembly B</p> <pre><code> public class MyModel : MyBaseEntity { public virtual int p1 { get; set; } public virtual int p2 { get; set; } public virtual DateTime p3 { get; set; } public virtual bool p4 { get; set; } private readonly ISet&lt;MyModelChild&gt; _p5; private readonly ISet&lt;MyModelChild2&gt; _p6; public virtual string p7 { get; set; } public MyModel() { this._p5 = new HashSet&lt;MyModelChild&gt;(); this._p6 = new HashSet&lt;MyModelChild2&gt;(); } public virtual IEnumerable&lt;MyModelChild&gt; P5 { get { return _p5; } } public virtual IEnumerable&lt;MyModelChild2&gt; P6 { get { return _p6; } } } </code></pre> <p>In the class MyBaseEntity I try to get the private ISet child and call the method "Add". I call the "addChild" method like</p> <pre><code>myObject.addChild&lt;MyModelChild&gt;(child); </code></pre> <p>but the <code>GetProperties</code> method doesn't extract the private property. It can extracts all the public properties but not the private.</p> <p>Anyone can help me?</p> <p>Thank you!</p>
 

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