6

Suppose i have this Enum:

namespace BusinessRule
{
    public enum SalaryCriteria : int
        {
            [EnumDisplayName(DisplayName = "Per Month")]
            Per_Month = 1,
            [EnumDisplayName(DisplayName = "Per Year")]
            Per_Year = 2,
            [EnumDisplayName(DisplayName = "Per Week")]
            Per_Week = 3
        }
}

I have its name in a string variable like :

string EnumAtt = "SalaryCriteria";

i am trying to check if this Enum is defined by this name, and if defined i want to get its instance.i have tried like this, but type is returning null:

string EnumAtt = "SalaryCriteria";
Type myType1 = Type.GetType(EnumAtt);

i have also tried this:

string EnumAtt = "BusinessRule.SalaryCriteria";
Type myType1 = Type.GetType(EnumAtt);

any idea how i can achieve this.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • @Rawling it is not a duplicate, i have seen these questions but my scenario is toally different – Ehsan Sajjad Aug 20 '14 at 12:07
  • @Rawling: He does not know the enum type at compile time. You can see that already from the different titles. – Tim Schmelter Aug 20 '14 at 12:09
  • @TimSchmelter you got it right, i want it to be on execution time – Ehsan Sajjad Aug 20 '14 at 12:09
  • To get a type by name you need to use something like [`Assembly.GetType(type-name)`](http://msdn.microsoft.com/en-us/library/y0cd10tb.aspx). – Richard Aug 20 '14 at 12:10
  • 2
    I reopened it, but the title was a bit misleading. – ken2k Aug 20 '14 at 12:10
  • @Richard i already showed in code i tried with ``GetType()`` but it is returning ``null`` – Ehsan Sajjad Aug 20 '14 at 12:11
  • @ken2k what is suitable title you can edit it. – Ehsan Sajjad Aug 20 '14 at 12:11
  • @Rawling you can see the code what i am trying to achieve, i have already saw ``Enum.Parse()`` but its not suitable in my scenario, because i have a lot of Enums and i want it at runtime – Ehsan Sajjad Aug 20 '14 at 12:14
  • This is a duplicate of [this](http://stackoverflow.com/questions/1825147/type-gettypenamespace-a-b-classname-returns-null) then. – Rawling Aug 20 '14 at 12:14
  • @Rawling i tried it but it returns ``null`` somewhere i am doing wrong but unable to get to it till – Ehsan Sajjad Aug 20 '14 at 12:15
  • I assume your type is defined in another assembly, and either the assembly is not referenced or you didn't specify the assembly qualified name. – ken2k Aug 20 '14 at 12:17
  • yes it is different assembly and my calling code is in different assembly, how to do in that case – Ehsan Sajjad Aug 20 '14 at 12:18
  • You need to walk all the loaded assemblies, which is shown here: http://stackoverflow.com/questions/458362/how-do-i-list-all-loaded-assemblies/8304132#8304132 – dbc Aug 20 '14 at 12:22
  • 1
    Also here: http://stackoverflow.com/questions/851248/c-sharp-reflection-get-all-active-assemblies-in-a-solution – dbc Aug 20 '14 at 12:24
  • Note what method I used: `Assemnbly.GetType` is *not* the same as `Type.GetType`. And, yes, this does mean you'll need to load the assembly to get a reference. – Richard Aug 20 '14 at 12:31

4 Answers4

15

To search all loaded assemblies in the current AppDomain for a given enum -- without having the fully qualified assembly name -- you can do:

    public static Type GetEnumType(string enumName)
    {
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            var type = assembly.GetType(enumName);
            if (type == null)
                continue;
            if (type.IsEnum)
                return type;
        }
        return null;
    }

For instance (picking a semi-random enum which is not in my assembly):

var type1 = Type.GetType("System.Xml.Linq.LoadOptions") // Returns null.
var type2 = GetEnumType("System.Xml.Linq.LoadOptions") // Returns successfully.

You name should still include the namespace.

dbc
  • 104,963
  • 20
  • 228
  • 340
4

A LINQ-inspired answer:

public static Type GetEnumType(string name)
{
  return 
   (from assembly in AppDomain.CurrentDomain.GetAssemblies()
    let type = assembly.GetType(name)
    where type != null
       && type.IsEnum
    select type).FirstOrDefault();
}

The reason is that you need to go through all loaded assemblies, not only the current assembly.

flindeberg
  • 4,887
  • 1
  • 24
  • 37
2

This works great for me.

Type myType1 = Type.GetType("BusinessRule.SalaryCriteria");

enter image description here

I tried it without "EnumDisplayName" attribute.

Igor
  • 281
  • 4
  • 12
0

This works well:

using System;

namespace BusinessRule
{
  public enum SalaryCriteria : int
  {
    Per_Month = 1,

    Per_Year = 2,

    Per_Week = 3
  }
}

namespace ConsoleApplication16
{
  internal class Program
  {
    private static void Main()
    {
      string EnumAtt = "BusinessRule.SalaryCriteria";
      Type myType1 = Type.GetType(EnumAtt);

      Console.WriteLine(myType1.AssemblyQualifiedName);
      Console.ReadLine();
    }
  }
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • The type is defined in a different assembly, so you'll have to use the assembly qualified name. – ken2k Aug 20 '14 at 12:24