问题
Is there a keyword
to refer to the current class type similar to how this
refers to the current instance?
My intention is to avoid having to type the full class name when refering to static members or Enums but I want to prefex it with something like "this" for readability like I do for instance members. The reason being some of the class names are pretty huge and cause excessive wrapping.
回答1:
I don't think there's a keyword, but maybe as good:
this.GetType();
GetType is one of the few methods implemented by System.Object
.
回答2:
If you include the namespace with using
, you won't indeed to enter the full path.
You can alias the class names with using
, for instance:
using ClassA = Really.Deep.Class.In.Namepsace.For.SomePurpose.ClassA;
回答3:
typeof(this)
will do what you're after.
Edit:
I stand corrected. typeof(this)
isn't valid C#. dbaseman is correct: this.GetType()
will work.
来源:https://stackoverflow.com/questions/13677237/c-sharp-keyword-similar-to-this-but-for-current-class-type