.net c#声明一个未知类型的列表并根据选择分配值

Declare a List of unknown type and assign the value based on the selection
2021-07-31
  •  译文(汉语)
  •  原文(英语)

我有不同类型的列表,根据所选列表,需要填充我的网格.这些列表项类的定义如下:

public class plist
{
    public DateTime date_modified { get; set; }
    public DateTime date_added { get; set; }
}

public class cList1 :plist
{
    public string code { get; set; }
    public string name { get; set; }
}

public class cList2 : plist
{
    public string typename { get; set; }
    public int typecode { get; set; }
}

在我的网页中,我需要根据选择的列表clist1或来填充控件clist2.

protected void Page_Load(object s,EventArgs e)
{
    List<cList1> codes = new List<cList1>();
    List<cList2> types = new List<cList2>();

    switch (getGrpSelValue())
    {
        case (int)LookupType.LU1:
            codes = getMyCodes(0, null, null);
            bindMyGrid(cList1, masterGrd);
            break;
        case (int)LookupType.LU2:
            types = getMyTypes(0, null, null);
            bindMyGrid(cList2, masterGrd);
            break;
        default:
            break;
    }
}

public void bindMyGrid<T>(List<T> list, GridView gv)
{
    //binds the grid here
}

有没有一种方法可以声明一个通用列表对象,然后为其分配代码或类型并bindMyGrid一次调用方法?

速聊1:
您是否尝试过使用ArrayList?
速聊2:
bindMyGrid(cList1, masterGrd);其中,cList1在这里,它不与方法的定义地图
速聊3:
不确定在此代码中您要优化/删除的内容吗?类型安全问题(如果不是HighCore的建议可以使用List<object>)?还是您只是想避免使用局部变量之类的BindMyGrid(GetMyTypes(0, null, null), masterGrid);?
速聊4:
没有那么多重复的代码...
速聊5:
我只列出了两个不同的列表,但是我们大约有18个列表,并且不想声明18个不同的列表,并根据切换的情况调用bindmygrid方法18次
解决过程1
var codes = new List<object>();

if (condition)
    codes.Add(someClass1);
else
    codes.Add(someClass2);

你可以将任何对象存储为object一个List<object>.

无论如何,我不明白这个目的.为什么不只用不同的参数调用泛型方法?

if (condition)
    BindMyGrid<Class1>(someListClass1);
else
    BindMyGrid<Class2>(someListClass2);
速聊1:
您可能还使用ArrayList... -1.
速聊2:
我倾向于避免这种情况.它让我想起了Java,这真令人恶心.
速聊3:
是的,它和同一件事一样有效.

I have different types of lists, and based on the selected list, my grid needs to be populated. These list item classes are defined as follows:

public class plist
{
    public DateTime date_modified { get; set; }
    public DateTime date_added { get; set; }
}

public class cList1 :plist
{
    public string code { get; set; }
    public string name { get; set; }
}

public class cList2 : plist
{
    public string typename { get; set; }
    public int typecode { get; set; }
}

In my web page, I need to populate the control based on the list selected clist1 or clist2.

protected void Page_Load(object s,EventArgs e)
{
    List<cList1> codes = new List<cList1>();
    List<cList2> types = new List<cList2>();

    switch (getGrpSelValue())
    {
        case (int)LookupType.LU1:
            codes = getMyCodes(0, null, null);
            bindMyGrid(cList1, masterGrd);
            break;
        case (int)LookupType.LU2:
            types = getMyTypes(0, null, null);
            bindMyGrid(cList2, masterGrd);
            break;
        default:
            break;
    }
}

public void bindMyGrid<T>(List<T> list, GridView gv)
{
    //binds the grid here
}

Is there a way to declare one generic list object and then assign code or types to it and call bindMyGrid method once?

Talk1:
Have you tried using an ArrayList ?
Talk2:
bindMyGrid(cList1, masterGrd); where cList1 in here, it does not map with definition of method
Talk3:
Not sure what you are trying to optimize/get rid of in this code? Is type safety concern (if not HighCore's suggestion to use List<object> is ok)? Or you are trying to simply avoid local variable like BindMyGrid(GetMyTypes(0, null, null), masterGrid);?
Talk4:
It isn't that much repeated code...
Talk5:
I just listed two different lists, but we have around 18 lists and didn't want to declare 18 different lists and call the bindmygrid method 18 times based on the switch case
Solutions1
var codes = new List<object>();

if (condition)
    codes.Add(someClass1);
else
    codes.Add(someClass2);

you can store any object as an object in a List<object>.

Anyways, I don't understand the purpose of this. Why don't you just call the generic method with different parameters?

if (condition)
    BindMyGrid<Class1>(someListClass1);
else
    BindMyGrid<Class2>(someListClass2);
Talk1:
You might as well be using an ArrayList... -1.
Talk2:
I tend to avoid that. It reminds me of java, which is disgusting.
Talk3:
Right, it's effective as the same thing.
转载于:https://stackoverflow.com/questions/17070076/declare-a-list-of-unknown-type-and-assign-the-value-based-on-the-selection

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入