1

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.
2
  • if you see doc: typeName: The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.
    – Grundy
    Commented Oct 6, 2015 at 7:16
  • You don't necessarily have to save the type name in a string. See my answer, and please clarify if there are requirements not mentioned in the question. Commented Oct 6, 2015 at 7:18

2 Answers 2

5

You can try to use Type Properties

typeof(T).Name

and if you are dealing with instance then

this.GetType().Name
2

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

var type1 = typeof(MyClass);
4
  • 2
    OP want save typename in string, and in this case your variant not work
    – Grundy
    Commented Oct 6, 2015 at 7:13
  • @Grundy the OP says "I need to get the type of the class in order to use it in reflection". There may not be a requirement to save the typename in a string. Commented Oct 6, 2015 at 7:17
  • I'll agree with Eren on this one, it just seems like OP thought it was the only way.
    – Bauss
    Commented Oct 6, 2015 at 7:17
  • I not thought about it :-) i think that just many types in string and trying work with it :-)
    – Grundy
    Commented Oct 6, 2015 at 7:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.