从 class file.dynamically 加载所有枚举
Load all enums from a class file.dynamically
这是我的 class 文件。
public class GetValuesTest {
enum Color { Red, Green, Blue, Yellow };
enum Styles { Plaid = 0, Striped = 23, Tartan = 65, Corduroy = 78 };
public static void Main() {
Console.WriteLine("The values of the Colors Enum are:");
foreach(int i in Enum.GetValues(typeof(Colors)))
Console.WriteLine(i);
Console.WriteLine();
Console.WriteLine("The values of the Styles Enum are:");
foreach(int i in Enum.GetValues(typeof(Styles)))
Console.WriteLine(i);
}
}
我不想像 Enum.GetValues(typeof(Colors)) 和样式这样硬编码枚举对象,而是想在运行时动态加载所有枚举并获取它们的值。请帮忙
您将不得不使用模板化方法或函数。喜欢 "open bracket" T "close bracket" 。然后编译器在编译时会枚举你传入的enumType。
然而,您现在已将枚举类型硬编码到外部调用代码中。
Enum 的要点是在 code/compile 时固定 Type。请考虑您的用例,因为您可能根本不需要枚举,而是更像数据的键值对。
(多一点..)
因此,在您的情况下,您会将颜色枚举加载到数组或列表中。对于 .NET,它们是颜色枚举 - 对您来说,在运行时它们是应用程序的 list/array 有效颜色选择,因此您将它们放入颜色类型列表的运行时变量中。列表和数组类型允许您在应用程序运行时以您需要的方式存储和引用。您的应用程序使用内置枚举类型范围作为数组,但这并不是枚举类型真正支持的内容。
我试过使用反射来做到这一点,而且很有可能。虽然代码不是很整洁漂亮。我相信它可以改进以提高生产质量。无论如何,下面是我用枚举声明的 class:
public class TestingEnums
{
public enum Color { Red, Blue, Yellow, Pink }
public enum Styles { Plaid = 0, Striped = 23, Tartan = 65, Corduroy = 78 }
public string TestingProperty { get; set; }
public string TestingMethod()
{
return null;
}
}
我创建了一个 winforms 按钮事件来获取其枚举的每个值。下面是事件处理程序代码,它以字符串形式显示枚举的每个成员。
private void btnTest_Click(object sender, EventArgs e)
{
var t = typeof (TestingEnums);
var nestedTypes = t.GetMembers().Where(item=>item.MemberType == MemberTypes.NestedType);
foreach (var item in nestedTypes)
{
var type = Type.GetType(item.ToString());
if (type!=null && type.IsEnum)
{
string items = " ";
foreach (MemberInfo x in type.GetMembers())
{
if (x.MemberType == MemberTypes.Field)
{
if (!x.Name.Equals("value__"))
{
items = items + (" " + Enum.Parse(type, x.Name));
items = items + (" " + (int)Enum.Parse(type, x.Name));
}
}
}
MessageBox.Show(items);
}
}
}
}
P.S。我相信我们可以做一些比字符串比较更好的事情来摆脱 value__ 字段。
这是我的 class 文件。
public class GetValuesTest {
enum Color { Red, Green, Blue, Yellow };
enum Styles { Plaid = 0, Striped = 23, Tartan = 65, Corduroy = 78 };
public static void Main() {
Console.WriteLine("The values of the Colors Enum are:");
foreach(int i in Enum.GetValues(typeof(Colors)))
Console.WriteLine(i);
Console.WriteLine();
Console.WriteLine("The values of the Styles Enum are:");
foreach(int i in Enum.GetValues(typeof(Styles)))
Console.WriteLine(i);
}
}
我不想像 Enum.GetValues(typeof(Colors)) 和样式这样硬编码枚举对象,而是想在运行时动态加载所有枚举并获取它们的值。请帮忙
您将不得不使用模板化方法或函数。喜欢 "open bracket" T "close bracket" 。然后编译器在编译时会枚举你传入的enumType。
然而,您现在已将枚举类型硬编码到外部调用代码中。
Enum 的要点是在 code/compile 时固定 Type。请考虑您的用例,因为您可能根本不需要枚举,而是更像数据的键值对。
(多一点..) 因此,在您的情况下,您会将颜色枚举加载到数组或列表中。对于 .NET,它们是颜色枚举 - 对您来说,在运行时它们是应用程序的 list/array 有效颜色选择,因此您将它们放入颜色类型列表的运行时变量中。列表和数组类型允许您在应用程序运行时以您需要的方式存储和引用。您的应用程序使用内置枚举类型范围作为数组,但这并不是枚举类型真正支持的内容。
我试过使用反射来做到这一点,而且很有可能。虽然代码不是很整洁漂亮。我相信它可以改进以提高生产质量。无论如何,下面是我用枚举声明的 class:
public class TestingEnums
{
public enum Color { Red, Blue, Yellow, Pink }
public enum Styles { Plaid = 0, Striped = 23, Tartan = 65, Corduroy = 78 }
public string TestingProperty { get; set; }
public string TestingMethod()
{
return null;
}
}
我创建了一个 winforms 按钮事件来获取其枚举的每个值。下面是事件处理程序代码,它以字符串形式显示枚举的每个成员。
private void btnTest_Click(object sender, EventArgs e)
{
var t = typeof (TestingEnums);
var nestedTypes = t.GetMembers().Where(item=>item.MemberType == MemberTypes.NestedType);
foreach (var item in nestedTypes)
{
var type = Type.GetType(item.ToString());
if (type!=null && type.IsEnum)
{
string items = " ";
foreach (MemberInfo x in type.GetMembers())
{
if (x.MemberType == MemberTypes.Field)
{
if (!x.Name.Equals("value__"))
{
items = items + (" " + Enum.Parse(type, x.Name));
items = items + (" " + (int)Enum.Parse(type, x.Name));
}
}
}
MessageBox.Show(items);
}
}
}
}
P.S。我相信我们可以做一些比字符串比较更好的事情来摆脱 value__ 字段。