Samantha J T Star
Samantha J T Star

Reputation: 32798

I'm unable to get a type using the Type.GetType method?

Sorry if the question sounds confusing. My problem is that I am using the following:

var packageType = Type.GetType(className); 

I have checked very carefully that className is the fully qualified name of a type. I checked the className variable many times. But still when this is executed it gives packageType as null !

I know my class name is Product. Is there a way that I can get a string representation of the name so I can check to see if it compares exactly with the className string I am passing to the above.

Upvotes: 1

Views: 847

Answers (3)

Anthony Pegram
Anthony Pegram

Reputation: 126844

You may need to provide the assembly qualified name. Note:

Type.GetType

Parameters: typeName

Type: System.String

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.

For example, if I am trying to get a type defined outside of the currently executing assembly, I could use Type.GetType as following:

var name = "CommonLibrary.ICommand, CommonLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
var type = Type.GetType(name);

If inside the executing assembly, I only need to qualify it via the namespace

var type = Type.GetType("CommonLibrary.ICommand");

Upvotes: 3

Mujtaba Hassan
Mujtaba Hassan

Reputation: 2493

You need to call this using an object e.g.

string str = "";
string type = str.GetType().ToString();

This will give you "System.String" in "type" variable

Upvotes: -1

Mike Nakis
Mike Nakis

Reputation: 61984

Yes: typeof(Product).FullName

Upvotes: 3

Related Questions