How to figure out whether an enum is defined in an assembly. [modified]
-
public class SomeEnumAttribute: DescriptionAttribute { public override string Description { get { string resFile; Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in assemblies) { Type type = assembly.GetType("/*The type defined needs to go in here*/"); //For eg: nameofdll.MyEnum if (type != null) { // the type passed in GetType() is defined in this assembly so get the resource file name for it resFile = assembly.GetName().Name ; // we have the name of the assembly - break now break; } } } } } //We use this class from another binary in which MyEnum is defined public enum MyEnum { [SomeEnumAttribute("abc")] abc = 0, [SomeEnumAttribute("def")] def, [SomeEnumAttribute("ghi")] ghi, [SomeEnumAttribute("ghi1")] ghi1 }
What we ought to do is this: Get the fully qualified name of the enum(MyEnum) and replace it with "/*The type defined needs to go in here*/" in the snippet above. Can someone help with this. Note: This class is defined in another binary. In another words the question is can we get the enum name(type name) in which the attribute is being used.
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
modified on Thursday, June 4, 2009 4:11 AM
-
public class SomeEnumAttribute: DescriptionAttribute { public override string Description { get { string resFile; Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in assemblies) { Type type = assembly.GetType("/*The type defined needs to go in here*/"); //For eg: nameofdll.MyEnum if (type != null) { // the type passed in GetType() is defined in this assembly so get the resource file name for it resFile = assembly.GetName().Name ; // we have the name of the assembly - break now break; } } } } } //We use this class from another binary in which MyEnum is defined public enum MyEnum { [SomeEnumAttribute("abc")] abc = 0, [SomeEnumAttribute("def")] def, [SomeEnumAttribute("ghi")] ghi, [SomeEnumAttribute("ghi1")] ghi1 }
What we ought to do is this: Get the fully qualified name of the enum(MyEnum) and replace it with "/*The type defined needs to go in here*/" in the snippet above. Can someone help with this. Note: This class is defined in another binary. In another words the question is can we get the enum name(type name) in which the attribute is being used.
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
modified on Thursday, June 4, 2009 4:11 AM
-
typeof(MyEnum).FullName
Between the idea And the reality Between the motion And the act Falls the Shadow
Thanks for the reply. The problem is that this MyEnum is defined elsewhere in another binary and not in this one.
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
-
Thanks for the reply. The problem is that this MyEnum is defined elsewhere in another binary and not in this one.
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
Then you need to call the code snippet I gave in that binary. If you don't have access to it, the format is
namespace.MyEnum
. Just make sure you know which namespace it's in An alternative is to scan the entire assembly, and wait until you get a Type which has IsEnum set, and has a Name of MyEnum. Then, note down the FullName property, and use that when you use Assembly.GetType. Don't do the scanning in your finished program though - just whip up a small testbed to give you the FullName, and use that in your actual codeBetween the idea And the reality Between the motion And the act Falls the Shadow
-
Then you need to call the code snippet I gave in that binary. If you don't have access to it, the format is
namespace.MyEnum
. Just make sure you know which namespace it's in An alternative is to scan the entire assembly, and wait until you get a Type which has IsEnum set, and has a Name of MyEnum. Then, note down the FullName property, and use that when you use Assembly.GetType. Don't do the scanning in your finished program though - just whip up a small testbed to give you the FullName, and use that in your actual codeBetween the idea And the reality Between the motion And the act Falls the Shadow
Just to make it clear - Are you assuming that you have the name of the Enum, in our case MyEnum, that can be searched in the assembly. The problem still remains on getting the name of the enum in the DescriptionAttribute derived class so that I can use it to locate the assembly in which it is defined. Thanks for the help... :)
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
-
Just to make it clear - Are you assuming that you have the name of the Enum, in our case MyEnum, that can be searched in the assembly. The problem still remains on getting the name of the enum in the DescriptionAttribute derived class so that I can use it to locate the assembly in which it is defined. Thanks for the help... :)
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
I'm still assuming that you want to get the type of MyEnum. The second paragraph shows how you could retrieve the full name. If you already know the full name (including namespace - you may be able to find this out using Red Gate's .Net Reflector), then you can pass this to Assembly.GetType
Between the idea And the reality Between the motion And the act Falls the Shadow
-
I'm still assuming that you want to get the type of MyEnum. The second paragraph shows how you could retrieve the full name. If you already know the full name (including namespace - you may be able to find this out using Red Gate's .Net Reflector), then you can pass this to Assembly.GetType
Between the idea And the reality Between the motion And the act Falls the Shadow
But to get the type we should have the name (namespace.MyEnum), which I don't have. I want to make it available in the DescriptionAttribute derived class.
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
-
But to get the type we should have the name (namespace.MyEnum), which I don't have. I want to make it available in the DescriptionAttribute derived class.
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
Exactly. If you open the file in .Net Reflector, then you can search for MyEnum and get the namespace from there. But why not just make Type of MyEnum available in the DescriptionAttribute derived class, instead of the name; that way you can save the time it takes to call Assembly.GetType. As long as you know how to create your own attribute which accepts a Type instantiation and has a Type property, and how to use Assembly.GetCustomAttributes, you have the Type
Between the idea And the reality Between the motion And the act Falls the Shadow
-
public class SomeEnumAttribute: DescriptionAttribute { public override string Description { get { string resFile; Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in assemblies) { Type type = assembly.GetType("/*The type defined needs to go in here*/"); //For eg: nameofdll.MyEnum if (type != null) { // the type passed in GetType() is defined in this assembly so get the resource file name for it resFile = assembly.GetName().Name ; // we have the name of the assembly - break now break; } } } } } //We use this class from another binary in which MyEnum is defined public enum MyEnum { [SomeEnumAttribute("abc")] abc = 0, [SomeEnumAttribute("def")] def, [SomeEnumAttribute("ghi")] ghi, [SomeEnumAttribute("ghi1")] ghi1 }
What we ought to do is this: Get the fully qualified name of the enum(MyEnum) and replace it with "/*The type defined needs to go in here*/" in the snippet above. Can someone help with this. Note: This class is defined in another binary. In another words the question is can we get the enum name(type name) in which the attribute is being used.
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
modified on Thursday, June 4, 2009 4:11 AM
Are you trying to write a generic attribute or one that only works with
MyEnum
? If it's the former, I can't see any way around passing typeof(MyEnum) to the constructor of SomeEnumAttribute.Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
Are you trying to write a generic attribute or one that only works with
MyEnum
? If it's the former, I can't see any way around passing typeof(MyEnum) to the constructor of SomeEnumAttribute.Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
there is a way though: SomeEnum[Description(typeof(MyEnum),"value")] write a constructor in SomeEnum attribute derived class and lo and behold you have the MyEnum type in the attribute class.
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
-
there is a way though: SomeEnum[Description(typeof(MyEnum),"value")] write a constructor in SomeEnum attribute derived class and lo and behold you have the MyEnum type in the attribute class.
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
Um, isn't that what I said in my previous post?
I can't see any way around passing typeof(MyEnum) to the constructor of SomeEnumAttribute.
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro