تسجيل الدخول

مشاهدة النسخة كاملة : [SOLVED] How do I get the type of the class I'm in without knowing its name? [modified]



C# Programming
02-21-2010, 03:22 AM
Hi everyone.

I'm creating a class through reflection and I'd like to have a static initializer like this:

class MyClass {
static MyClass() {
someMethod(typeof(MyClass));
}
}
That is, I'd like to obtain the type of MyClass and pass it to someMethod as argument. What's giving me trouble is obtaining the type of MyClass, since I'm emitting this code before the type MyClass is created: I'm defining MyClass with a TypeBuilder, but I haven't called CreateType() yet because I first need to add the static initializer. I have a hen-and-egg situation here.

Ideally what I'd like is to do something like:

class MyClass {
static MyClass() {
someMethod(this.GetType());
}
}
but of course not with this because I'm in a static member and there's no "this". So, is there something *****alent to this but that identifies the class (or type) in which the code is declared (i.e., to which class belongs the code that is being run)? Sorry if I don't make much sense, I'm having trouble expressing this clearly http://www.barakasoft.com/script/Forums/Images/smiley_redface.gif

Thanks!modified on Friday, February 19, 2010 8:35 AM