动态清单 [英] Dynamic List

查看:153
本文介绍了动态清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有两个或两个以上的类,分别称为Dashboard,Order,Shipment等.我有一个函数来获取所有模块的列表,在这些模块中,我将使用相同的类.
例如,

Hi All,
I have two or more classes called Dashboard, Order, Shipment and etc. I have a function to get list of all modules where i will be use same classes.
For example,

public class Dashboard
    {
        public string Key { get; set; }
        public string Name { get; set; }
    }
    public class Order
    {
        public string OrderNo { get; set; }
        public string Buyer { get; set; }
    }
    public class Shipment
    {
        public string ShipmentNo { get; set; }
        public string Location { get; set; }
    }

 public List<object> GetList()
        {
<!-- Get List Code Here-->return lstobject;
        }


是否可以调用GetList()方法并返回所需的列表对象.例如,如果我想要仪表板列表,则返回List<Dashboard>如果我想要订单列表,则返回List<Order>.

请任何人帮助我使用这种方法.


Is there possible to call GetList() method and return the wanted list object. For example, if i want dashboard list then return List<Dashboard> If i want order list then return List<Order>.

Please anyone help me to use this type of method.

推荐答案

下面的代码将验证列表中的对象是否为仪表板,它将创建Dashboard类的对象.同样也可以应用于其他类.

The below code will verify if the object in list is dashboard, it will create the object of Dashboard class. The same can be applied on other classes too.

public static List<object> GetList()
{
   // Get List Code Here-->
   List<object> lstobject = new List<object>();
   lstobject.Add(new Dashboard());

   return lstobject;
}


要获取对象,请使用以下代码块


To get the object use the following block

List<object> lstObject = GetList();
Dashboard ds=null;
for (int i = 0; < lstObject.Count; i++)
{
    if (lstObject[i] is Dashboard)
    {
        ds = (Dashboard)lstObject[i];
        Console.WriteLine(ds.Key);
    }
}


希望这个回答您的问题


Hope this answer your question


您好,

您可以像这样使用.

Hi,

You can Use Like this.

public class getListClass
    {
        public static IList   getList()
        {
            List<Shipment> _lst = new List<Shipment>();
            Shipment _sh = new Shipment();
            _lst.Add(_sh);
            return _lst;             

        }
    }





List<Shipment> _ss = (List<Shipment>)getListClass.getList();



我声明静态仅用于测试.您也可以使用IEnumerable代替IList.



I have declare static only for test. You can use IEnumerable also instead of IList.


这篇关于动态清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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