Programming VB.Net

VB.Net: Get type in shared (static) context

Before I start I have to clear one thing, but I do not want to explain it:  that what static means in Delphi is Shared in VB.Net. When you do not know what I am talking about here then please read the documentation!

In Delphi it’s easy to access the class itself (not the instance!) in which a static method is declared: you can use Self to access the class and its static methods and properties. Imho it’s not that easy in VB.Net! I tried some things until is stumbled over this thread. I know that there is the solution for my problem, so why do I post the solution once more? The first reason is that I want to say that the solution postet there works ;) And second one is that I want to spread the solution because I haven’t found it very often via Google.

So here it is: you can access the type by importing System.Reflection and using MethodBase.GetCurrentMethod().DeclaringType.

Update – 26.04.2012

Well, I thought that I found the solution for my problem, but hey, it is not that simple… MethodBase.GetCurrentMethod().DeclaringType returns the type in which the static/shared property/method is declared, not the type of the “current” class. So if you inherit a class be careful!

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha * Time limit is exhausted. Please reload CAPTCHA.