Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过在字符串中指定枚举类型的名称来获取枚举类型_C#_Reflection_Types_Enums - Fatal编程技术网

C# 如何通过在字符串中指定枚举类型的名称来获取枚举类型

C# 如何通过在字符串中指定枚举类型的名称来获取枚举类型,c#,reflection,types,enums,C#,Reflection,Types,Enums,假设我有这个枚举: namespace BusinessRule { public enum SalaryCriteria : int { [EnumDisplayName(DisplayName = "Per Month")] Per_Month = 1, [EnumDisplayName(DisplayName = "Per Year")] Per_Year = 2,

假设我有这个枚举:

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
        }
}
我将其名称放在一个字符串变量中,如:

string EnumAtt = "SalaryCriteria";
我正在尝试检查此枚举是否由此名称定义,如果已定义,我希望获取其实例。我已尝试过这样做,但
type
返回
null

string EnumAtt = "SalaryCriteria";
Type myType1 = Type.GetType(EnumAtt);
我也试过:

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

你知道我怎样才能做到这一点吗。

这对我来说非常有用

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

我在没有“EnumDisplayName”属性的情况下尝试了它。

这很有效:

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();
    }
  }
}

要在当前AppDomain中搜索所有加载的程序集以查找给定的枚举(不具有完全限定的程序集名称),可以执行以下操作:

    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;
    }
例如(拾取不在我的程序集中的):


您的名字仍然应该包括名称空间。

一个受LINQ启发的答案:

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();
}

原因是您需要检查所有加载的程序集,而不仅仅是当前程序集。

@Rawling它不是重复的,我已经看到了这些问题,但我的场景是different@Rawling:他在编译时不知道枚举类型。您可以从不同的标题中看到这一点。@TimSchmelter您说得对,我希望它在执行时重新打开,但标题有点误导。另外:类型是在不同的程序集中定义的,因此您必须使用程序集限定名。类型是在不同的程序集中定义的,因此,您必须使用程序集限定名称。@ken2k如何知道程序集名称程序集名称是什么?
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();
}