In order to use reflection in .NET, you need to get an object of class Type. I will explain how to use the Type object later, but first, you need to be able to get it.
There are 3 ways to get a Type in .NET.
- VB.NET has a GetType keyword which takes a class as a parameter. eg. you can writeDim T as Type = GetType(String)
This is not really a function, because you are passing in a class, not an actual value, and not even the name of a class! C# has a similar keyword, which I think is called typeof.
- All .NET objects inherit from Object. Object has a function defined on it called GetType, so all you need to do is eg.Dim p as New Person()
Dim T as Type = p.GetType()Note that if you have an Interface reference you may need to cast it using the CType operator in order to access the GetType function. You should be able to cast any .NET object to Object.
- You can get a Type by name from an Assembly. I will post details on this in a later post on Assemblies.
- On Reflection Part 1: What is Reflection?
- On Reflection Part 2: GetType