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. 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
Sign up to request clarification or add additional context in comments.

Comments

2

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

var type1 = typeof(MyClass);

4 Comments

OP want save typename in string, and in this case your variant not work
@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.
I'll agree with Eren on this one, it just seems like OP thought it was the only way.
I not thought about it :-) i think that just many types in string and trying work with it :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.