Class name for current instance

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

How do I get the class name for a current instance. For example, if I want
to know the Class Name for the current form, how do I get this
programatically.

Thanks
 
Hi,

You can use System.Reflection.MethodBase's [1] shared GetCurrentMethod
method to obtain information regarding the currently executed method. The
DeclaringType [2] property returns the type where the method is defined.

Sample code:

Imports System
Imports System.Reflection

Class TestClass
Shared Sub Main(ByVal args() as String)
Dim t as Type = MethodBase.GetCurrentMethod().DeclaringType
If Not (t is Nothing) Then
Console.WriteLine(t.FullName) ' Will output 'TestClass'
End If
End Sub
End Class

[1]:
http://msdn2.microsoft.com/en-US/library/system.reflection.methodbase(VS.80).aspx
[2]:
http://msdn2.microsoft.com/en-us/library/system.reflection.memberinfo.declaringtype.aspx
 
That will get the name of type that declares the method - which could be an
ancestor. That's not the same as the name of the type of the current
instance.

Also, FullName gives the fully qualified name - including namespace.

To get just the name of the current instance:

Dim typeName as String = Me.GetType().Name

Stanimir Stoyanov said:
Hi,

You can use System.Reflection.MethodBase's [1] shared GetCurrentMethod
method to obtain information regarding the currently executed method. The
DeclaringType [2] property returns the type where the method is defined.

Sample code:

Imports System
Imports System.Reflection

Class TestClass
Shared Sub Main(ByVal args() as String)
Dim t as Type = MethodBase.GetCurrentMethod().DeclaringType
If Not (t is Nothing) Then
Console.WriteLine(t.FullName) ' Will output 'TestClass'
End If
End Sub
End Class

[1]:
http://msdn2.microsoft.com/en-US/library/system.reflection.methodbase(VS.80).aspx
[2]:
http://msdn2.microsoft.com/en-us/library/system.reflection.memberinfo.declaringtype.aspx
--
Stanimir Stoyanov
www.stoyanoff.info


news.microsoft.com said:
How do I get the class name for a current instance. For example, if I
want
to know the Class Name for the current form, how do I get this
programatically.

Thanks
 
Marina,

Marina Levit said:
That will get the name of type that declares the method - which could be
an ancestor. That's not the same as the name of the type of the current
instance.

That's true. However, it makes sense when dealing with shared members as
'Me.GetType()' doesn't work inside these methods :-).
 
Right, the question did specifically ask how to get the name of the 'current
instance', so that implied there was an instance in question. So I just
responded to that.
 
Thank you all very much for this information, it has helped me progress with
what I was doing.

It's nice to see that people can be so helpful.




Marina Levit said:
Right, the question did specifically ask how to get the name of the
'current instance', so that implied there was an instance in question. So
I just responded to that.
 
Marina Levit said:
Right, the question did specifically ask how to get the name of the
'current instance', so that implied there was an instance in question. So
I just responded to that.

There is nothing wrong with your reply! I simply wanted to make the OP
aware that there are certain cases where the method described by Stanimir
makes sense.
 
Back
Top