C# 在方法的参数中使用动态枚举作为类型
我在这里试图实现的目标有点棘手。在开始之前,让我先简单介绍一下背景知识 我知道我们可以使用枚举作为方法参数的类型。例如,我可以做这样的事情(一个非常基本的例子)C# 在方法的参数中使用动态枚举作为类型,c#,C#,我在这里试图实现的目标有点棘手。在开始之前,让我先简单介绍一下背景知识 我知道我们可以使用枚举作为方法参数的类型。例如,我可以做这样的事情(一个非常基本的例子) 尝试使用该类,但找不到上面定义的“已完成”枚举。i、 我不能做以下事情 公共静态无效测试dynenum(Test.dynamiceneum.finished\u finished) { //完成后在这里做任何事。 } 我想这篇文章太长了,但我希望我已经说得很清楚了。你要么传入一个对象,要么动态创建你的方法 我可以问一下,为什么不能只使用
我想这篇文章太长了,但我希望我已经说得很清楚了。你要么传入一个对象,要么动态创建你的方法
我可以问一下,为什么不能只使用
int
?似乎您必须动态创建大量代码才能将其作为枚举传递。动态创建枚举时,只有运行代码,它才会存在。由于不能在编译时使用该类型,因此不能指定该类型的参数。只有当您已经有一个方法需要某种类型的枚举(但不是任何特定的枚举类型)时,或者如果您还创建了动态使用枚举的代码,则动态创建枚举才有用
使用enum实例基本上可以获得值的字符串表示形式,将字符串解析回值,并获得已定义值的列表。使用
字典
这样的工具更容易实现这一点。还有一个类似的so问题:我不能在这里使用int,因为我需要intellisense中的值(“在我的例子中是value1”和“value2”),如果我使用int,则无法获得这些值。此外,value1和value2是动态生成的,不能在静态枚举中硬编码。我已经修改了我的帖子以合并相同的内容。当您在运行时动态生成代码时,您在intellisense中无法获得这一点。如果您正在预生成一个程序集,并添加对该程序集的引用,那么您使用枚举作为参数类型的方法应该可以很好地工作。Hi-Lasse,我知道呆在同一个程序集中不起作用,所以我将创建枚举部分分离到另一个程序集中,并在单独的程序集中使用相同的方法。仍然无法找到枚举。是否正在生成枚举、保存程序集并创建对程序集的引用?或者你只是在运行时创建枚举?嗨,很抱歉这么晚才回复这个线程。正如您所建议的,我可以通过将生成动态枚举的代码分离到一个单独的dll,然后将dll的引用提供给主程序集来完成这项工作。谢谢你的回复。你可以把你的评论作为答案,这样其他人也可以从中受益。嗨,Guffa,我明白你在这里想说的了。我将创建一个字典,其中包含我希望在任何开发人员尝试使用我的方法时显示的值。这里只有一个问题。创建方法时,如何在参数中使用此字典?请给我一些建议。
namespace Test
{
class DefineEnums
{
public enum MyEnum
{
value1 = 0,
value2 = 1
}
}
class UseEnums
{
public void UseDefinedEnums(DefineEnums.MyEnum _enum)
{
//Any code here.
}
public void Test()
{
// "value1" comes here with the intellisense.
UseDefinedEnums(DefineEnums.MyEnum.value1);
}
}
}
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
namespace Test
{
public static class DynamicEnum
{
public static Enum finished;
static List<string> _lst = new List<string>();
static DynamicEnum()
{
_lst.Add("value1");
_lst.Add("value2");
finished = CreateDynamicEnum(_lst);
}
public static Enum CreateDynamicEnum(List<string> _list)
{
// Get the current application domain for the current thread.
AppDomain currentDomain = AppDomain.CurrentDomain;
// Create a dynamic assembly in the current application domain,
// and allow it to be executed and saved to disk.
AssemblyName aName = new AssemblyName("TempAssembly");
AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(
aName, AssemblyBuilderAccess.RunAndSave);
// Define a dynamic module in "TempAssembly" assembly. For a single-
// module assembly, the module has the same name as the assembly.
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");
// Define a public enumeration with the name "Elevation" and an
// underlying type of Integer.
EnumBuilder eb = mb.DefineEnum("Elevation", TypeAttributes.Public, typeof(int));
// Define two members, "High" and "Low".
//eb.DefineLiteral("Low", 0);
//eb.DefineLiteral("High", 1);
int i = 0;
foreach (string item in _list)
{
eb.DefineLiteral(item, i);
i++;
}
// Create the type and save the assembly.
return (Enum)Activator.CreateInstance(eb.CreateType());
//ab.Save(aName.Name + ".dll");
}
}
}
public static void TestDynEnum(Test.DynamicEnum.finished _finished)
{
// Do anything here with _finished.
}