Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Is there anyone who can show me how to implement a zero argument CreateLog() that gets the name from the subclass and not the declaring class?</p> </blockquote> <p>I don't think you'll be able to do it by looking at the stack frame. </p> <p>While your class is <code>IMReceiver</code>, the call to <code>CreateLog</code> method is in the <code>MessageReceiver</code> class. The stack frame must tell you where the method is being called from, or it wouldn't be any use, so it's always going to say <code>MessageReceiver</code></p> <p>If you called <code>CreateLog</code> explicitly in your <code>IMReceiver</code> and other classes, then it works, as the stack frame shows the method being called in the derived class (because it actually is).</p> <p>Here's the best thing I can come up with:</p> <pre><code>class BaseClass{ public Log log = Utils.CreateLog(); } class DerivedClass : BaseClass { public DerivedClass() { log = Utils.CreateLog(); } } </code></pre> <p>If we trace creation of logs, we get this:</p> <pre><code>new BaseClass(); # Log created for BaseClass new DerivedClass(); # Log created for BaseClass # Log created for DerivedClass </code></pre> <p>The second 'log created for derived class' overwrites the instance variable, so your code will behave correctly, you'll just be creating a BaseClass log which immediately gets thrown away. This seems hacky and bad to me, I'd just go with specifying the type parameter in the constructor or using a generic. </p> <p>IMHO specifying the type is cleaner than poking around in the stack frame anyway</p> <p>If you can get it without looking at the stack frame, your options expand considerably</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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