Type.GetType(enumName) fails - why?

  • Thread starter Thread starter Axel Dahmen
  • Start date Start date
A

Axel Dahmen

Hi,

I'd like to create a generic algorithm to analyse an enum's elements for
custom attributes. All I have at hand is is a type name to create an enum
type info from. But if I call Type.GetType(enumTypeName) it returns null or
throws an exception.

Can anyone enlighten me on how to walk through an unknown enums elements to
retrieve their custom attributes?

TIA,
Axel Dahmen
 
Axel Dahmen said:
I'd like to create a generic algorithm to analyse an enum's elements for
custom attributes. All I have at hand is is a type name to create an enum
type info from. But if I call Type.GetType(enumTypeName) it returns null or
throws an exception.

Can anyone enlighten me on how to walk through an unknown enums elements to
retrieve their custom attributes?

Which assembly is the enum in? If you only specify the name,
Type.GetType will look in the current assembly and mscorlib, but that's
all.
 
Hi Jon,

thanks for your reply! Indeed, like you said, I've just noticed that I can't
load *any* type from one of the other assemblies.

My current web site (ASP.NET 2.0) project uses .NETTIERS as ORM. I'd like to
walk through one of these type enums from the Entity assembly to determine
which columns to show in a GridView, using the TypeName I get from the
ObjectDataSource.

Your help is quite appreciated!

Regards,
www.axeldahmen.de
Axel Dahmen
 
Using the code below, triggering new ClassB(), t1 will be null while t2 will
contain a type. What would be the proper name for EnumA?

namespace Space
{
public class ClassA
{
public enum EnumA
{
ValA1,
ValA2,
}
}

public enum EnumB
{
ValB1,
ValB2,
}

public class ClassB
{
public ClassB()
{
Type t1 = Type.GetType("Space.ClassA.EnumA");
Type t2 = Type.GetType("Space.EnumB");
}
}
}
 
Morten Wennevik said:
Using the code below, triggering new ClassB(), t1 will be null while t2 will
contain a type. What would be the proper name for EnumA?

Space.ClassA+EnumB
 
You can analyze the names given to the types, just check the values for
"nameA" and "nameB":

string nameA = typeof(ClassA.EnumA).FullName;
string nameB = typeof(EnumB).FullName;

Type t1 = Type.GetType(nameA);
Type t2 = Type.GetType(nameB);


Morten Wennevik said:
Using the code below, triggering new ClassB(), t1 will be null while t2
will
contain a type. What would be the proper name for EnumA?

namespace Space
{
public class ClassA
{
public enum EnumA
{
ValA1,
ValA2,
}
}

public enum EnumB
{
ValB1,
ValB2,
}

public class ClassB
{
public ClassB()
{
Type t1 = Type.GetType("Space.ClassA.EnumA");
Type t2 = Type.GetType("Space.EnumB");
}
}
}

--
Happy Coding!
Morten Wennevik [C# MVP]


Jon Skeet said:
Which assembly is the enum in? If you only specify the name,
Type.GetType will look in the current assembly and mscorlib, but that's
all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top