Proposal: allow typeof(this) to reference the current class #8410
Replies: 4 comments 10 replies
-
Wouldn't |
Beta Was this translation helpful? Give feedback.
-
My problem with this proposal is that the behavior I actually expect requires reflection, or a hidden type parameter. I expect this sample to print var d = new Derived();
d.M();
public class Base
{
public void M() => Console.WriteLine(typeof(this).Name);
}
public class Derived : Base {} |
Beta Was this translation helpful? Give feedback.
-
Why hasn't anyone written a source generator for this yet? Copy-pasting is a thing of the past now. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
For initialization of a
log4net
logger, you have two suboptimal options:Option 1
This works well, but you need to remember to adjust
MyClass
every time you copy that line (which happens a lot).Option 2
This can be copy pasted, but it's quite convoluted and it uses reflection to do what could be known at compile time.
Allowing to write
would mean the compiler would use the innermost class that the code is currently wrapped in. I know that
this
doesn't make sense in a static context, but allowing that keyword to appear within atypeof()
expression, reading as "type of this" should be clear enough. It's basically the rough equivalent of VB.NET'sMyClass
keyword.Beta Was this translation helpful? Give feedback.
All reactions