Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get a reference to an instance of a derived class from within a base class
    primarykey
    data
    text
    <p>.Net 3.5, using C#</p> <p>I have an instance of a class (A) that has been called via a virtual method from a derived class (B). Within the method (a method of class A) I have discovered the Type of class B that has made the call, and using reflection I have examined class B and discovered a property whose value I want to access.</p> <p>Is there any way via reflection to get the instance reference to class B? The only object I have is my ‘this’ reference.</p> <p>[ Edit ]</p> <p>By way of explaining that I'm not totally nuts for needing to do this: My class is T4NmpBase (class A). I have no control over the class in which I am inheriting (TextTransformation) or the class that inherits from me (TextTemplating - class B). I would like to use the "Host" property if it exits but do not want to put any burden on the programmer who is writing the text templating code that generates the TextTransformation class.</p> <pre><code>// // this is my class // public abstract partial class T4NmpBase : Microsoft.VisualStudio.TextTemplating.TextTransformation { public override void Initialize() { // // determine derived class info and get value of "Host" property if it exists // } } // // this class is generated by T4 in a project that I have no control over // [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "10.0.0.0")] public partial class PPTest : T4_NMP_Base.T4NmpBase { public virtual global::Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost Host { ... } public override void Initialize() { base.Initialize(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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