简体   繁体   English

从多个不同的列表创建通用列表

[英]Create a generic list from multiple different lists

I am writing a Silverlight and WCF project. 我正在编写一个Silverlight和WCF项目。

I have used the Entity Framework in Silverlight to get the data from the database, I have multiple pages where I use EF. 我已经在Silverlight中使用实体框架从数据库中获取数据,我在多个页面中使用EF。

Now, I have more than 100 types of entity class and hence multiple known list types. 现在,我有100多种类型的实体类,因此有多种已知的列表类型。

In my WCF interface I want to create one generic function where I can accept all this list types as one generic parameter. 在WCF界面中,我想创建一个通用函数,在其中我可以接受所有这些列表类型作为一个通用参数。

So my WCF function should be like 所以我的WCF函数应该像

public string TestBadal(List<Object> list)
    {
        return "test";

    }

My question here is, how can I cast all the known lists to List. 我的问题是,如何将所有已知列表转换为List。 I am fairly new to this so any help will be much appreciated. 我对此还很陌生,因此我们将不胜感激。

Thank you. 谢谢。

you can use T 你可以用T

public static List<T> o<T>(List<T> a)
        {
             return a = new List<T>();
        }

As Sophex states you can write a generic function to process a List<> or better still an IList<> of anything. Sophex所述,您可以编写通用函数来处理List<>或更好的IList<>

So your example would become, 所以你的例子将变成

public string TestBadal<T>(IList<T> list)
{
    return "test";
}

This is very generic and requires and implies little about T . 这非常通用,几乎不需要T This may be sufficient for the processing you want to perform but, you don't say. 这对于您要执行的处理可能就足够了,但是,您不必说。

In general, you should view your method signature as a promise that the caller has to make to the function. 通常,您应该将方法签名视为调用者必须对该函数做出的承诺。 You should limit the promise to only what is required for your function to do its job. 您应该将诺言限制为仅执行功能所需的功能。 This way, the promise is easier to make and your function can get reused more with less commitment from the caller. 这样,更容易实现promise,并且可以以更少的调用者承诺来重用您的函数。

Your function as it stands, doesen't actually need a parameters and would be better defined as a string const but say, all you wanted to do was enumerate the items you could use, 就目前而言,您的函数实际上不需要参数,最好将其定义为string const但是说,您要做的就是枚举可以使用的项目,

public string TestBadal<T>(IEnumerable<T> entities)
{
    foreach(T entity in entities)
    {
        ...
    }
}

If your processing is especially related to the EF nature of the data source you could do, 如果您的处理特别与数据源的EF性质有关,

public string TestBadal<TEntity>(EntitySet<TEntity> entities)
    where TEntity : class
{
    ...
}

If you need to know somthing about the type in the list then you have two sensible options. 如果您需要了解列表中的类型,则有两个明智的选择。 Either you require the types to implement a certain interface, this essentialy makes your function non-generic and could be awkward with in conjunction with EF. 要么您需要类型来实现某个接口,要么本质上使您的函数不通用,并且与EF结合使用可能会很尴尬。

public string TestBadal(IEnumerable<IDefinedType> definedTypeInstances)
{
    foreach(IDefinedType instance in definedTypeInstances)
    {
       var x = instance.SomeDefinedProperty;
    }
}

Or, you can take the non generic parts of the function as a typed delegate parameter, keeping the function generic, possibly somthing like this. 或者,您可以将函数的非泛型部分作为类型化的委托参数,以使函数保持泛型,甚至可能是这样。

public string TestBadal<T>(
        IList<T> list,
        Func<T, string> stringSelector)
{
    var result = new StringBuilder();

    for(var i = 0; i < list.Count; i++)
    {
        result.AppendLine(stringSelector(list[i])
    }

    return result.ToString();
}

You could call this function with a lambda expression somthing like this, 您可以使用类似这样的lambda表达式来调用此函数,

var result = TestBadal(entities, e => e.SomeStringProperty);

I hope this answer both gives you some ideas and illustrates my point that the right answer depends on what you want your function to achieve. 我希望这个答案既能给您一些想法,又能说明我的观点,正确的答案取决于您要实现的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM