动态枚举作为类参数传递以创建字典 [英] Dynamic enum passed as class parameter to create dictionary

查看:112
本文介绍了动态枚举作为类参数传递以创建字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i我不确定这是否可能,而且我已经尝试过但似乎无法随时随地使用它:



i有几个子类执行一堆sql语句并在子类中保存值,一个缺点是我必须在每个类下设置属性来保存值。



我的想法在哪里创建一个可以继承的类,或者我可以传递一个enum的内容类(enum包含所有的字段名和序数),然后子类将创建该枚举的字典和字符串以保存值。



通过创建字典,我可以使用一个泛型类来保存所有参数,而不必写出我所拥有的每个类的属性和类,只是试图避免重复的代码到处(宠物讨厌)



为什么写当你可以写一个适应性的课程时,有20次满足您的需求! :)



所以,只是为了澄清。我希望将一堆不同的枚举传递给相同的类实例化方法,然后将其转换为传递的枚举和字符串的字典,但由于传递了不同的枚举,如果有意义,它必须在运行中完成?所以一旦字典被填充,我使用 Dictionary [] 然后它会给我带来enum成员列表?



报告'EnumObj'

Hi,

i am not sure if this is at all possible, and i have tried myself but cannot seem to get anywhere with it:

i have several sub classes that perform a bunch of sql statements and hold values in sub classes, one downside to this is that i have to have to setup properties under each class to hold the values.

My thoughts where to create a class that can be inherited or something to the parent class that i could pass an enum (enum contains all the field names and ordinal), the child class would then create a dictionary of that enumeration and string to hold the values.

By creating the dictionary as such i can use one generic class to hold all the parameters without having to write out the properties and the class in each class that i have, just trying to avoid repeated code all over the place (pet hate)

why write something 20 times when you can write an adaptable class to serve your needs! :)

So, just to clarify. I am looking to pass a bunch of different enums to the same class instantiation method and then convert this to a dictionary of the passed enum and string but due to different enums being passed it has to be done on the fly if that makes sense? So once the dictionary is populated, and i use Dictionary[] it then brings me the list of enum members?

As it statnds the 'EnumObj'

this.objDict = new Dictionary<EnumObj, String>();





突出显示错误消息



highlights witht his error message

The type or namespace name 'EnumObj' could not be found (are you missing a using directive or an assembly reference?)





以下是我正在使用的基本示例代码



Here is the base example code i am working with

class ParentClass
{
    /// <summary>
    /// Holds the properties
    /// </summary>
    public ChildClass Properties;

    public ParentClass()
    {
        this.Properties = new ChildClass(typeof(MyEnum));
    }

    enum MyEnum
    {
        col1 = 1,
        col2 = 2,
        col3 = 3
    }



}
/// <summary>
/// Holds the properties values for the array record
/// </summary>
public class ChildClass
{

    private Object objDict;

    public ChildClass(Type EnumObj)
    {
        this.objDict = new Dictionary<EnumObj, String>();
    }
    public ChildClass(Object EnumObj)
    {
        this.objDict = new Dictionary<typeof(EnumObj), String>();
    }
}





我尝试了什么:



传递方法的不同变化,我尝试将枚举作为对象而不是类型传递然后转换它。



What I have tried:

Different Variations on passing the method, i have trying passing the enum as an object rather than a type and then convert it.

推荐答案

由于您使用的是通用字典,因此子类应该是通用的。

试试这个:

Since you're using generic dictionary the child class should be generic.
Try this:
/// <summary>
/// Holds the properties values for the array record
/// </summary>
public class ChildClass<TEnum>
{
    private Dictionary<TEnum,String> objDict;
 
    public ChildClass()
    {
        this.objDict = new Dictionary<TEnum,String>();
    }
}

class ParentClass
{
    /// <summary>
    /// Holds the properties
    /// </summary>
    public ChildClass<MyEnum> Properties;
 
    public ParentClass()
    {
        this.Properties = new ChildClass<MyEnum>();
    }
 
    enum MyEnum
    {
        col1 = 1,
        col2 = 2,
        col3 = 3
    } 
}


这篇关于动态枚举作为类参数传递以创建字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆