Sonja
Sonja

Reputation: 595

dynamic class name for reflection in c#

I have a class :

Class MyClass
{
...
}

I need to get the type of the class in order to use it in reflection:

string className="MyClass";
var type1=Type.GetType(className, true); //I have a problem loading the class here.

Upvotes: 1

Views: 721

Answers (2)

Eren Ersönmez
Eren Ersönmez

Reputation: 39085

You don't necessarily need the name, you can directly do:

var type1 = typeof(MyClass);

Upvotes: 2

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

You can try to use Type Properties

typeof(T).Name

and if you are dealing with instance then

this.GetType().Name

Upvotes: 5

Related Questions