473,383 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Type.GetType(enumName) fails - why?

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
Dec 6 '07 #1
4 13087
Axel Dahmen <ke********@newsgroup.nospamwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 6 '07 #2
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 [C# MVP]" wrote:
Axel Dahmen <ke********@newsgroup.nospamwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 6 '07 #3
Morten Wennevik [C# MVP] <Mo************@hotmail.comwrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 6 '07 #4
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 [C# MVP]" <Mo************@hotmail.comwrote in message
news:3E**********************************@microsof t.com...
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 [C# MVP]" wrote:
>Axel Dahmen <ke********@newsgroup.nospamwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Dec 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AlexS | last post by:
I have app, which has usings for System.Windows.Forms But when I do Type.GetType("TreeView") or use full class name for TreeView I get null. Why? Anything I miss? What are the rules of use...
1
by: Jean Stax | last post by:
Hi ! I created a sample library project. In my second project I reference this library and make the following call, which returns "undefined value": Type myType =...
4
by: Mike Walters | last post by:
I have Two Project in one Solution. I am trying to use Type.GetType("string") to pass the "Type" to a Sub. Project one is MWDNav. Project two is MWDBilling. MWDNav is my exe. MWDBing is a Class...
6
by: Raghu | last post by:
In C#, the typeof keyword can be used to get a type of the class. This does not require object to be created first. However O am not sure how do the same thing in vb. I don't want to create the...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
16
by: Andrew Ducker | last post by:
Is there any way to use Type.GetType to return a type that's in an Assembly loaded from disk? I sadly _have_ to use Type.GetType, as it's in framework code I can't change. At the moment, I'm...
5
by: pinkfloydfan | last post by:
Hi there I have the following situation...I want to perform an operation on an Enum parameter but I don't know which one of a number of Enums it will be on. The function would include...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
9
by: Tony Johansson | last post by:
Hello! If I have string name = "Mytest"; and Type test1 = Type.GetType(name); and Type test2 = name.GetType(); then the first one will assign null to test1 but the second test2 will be given...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.