有没有办法将通用列表转换为接口/基类类型列表? [英] Is there a way to cast generic lists to lists of interface/base class types?

查看:111
本文介绍了有没有办法将通用列表转换为接口/基类类型列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图向他人展示他们创建的疯狂情况下的界​​面。它们在列表中有几个不相关的对象,并且需要对每个对象的两个字符串属性执行操作。我指出,如果他们将属性定义为接口的一部分,他们可以使用接口对象作为作用于其上的方法参数的类型;例如:

  void PrintProperties(IEnumerable< ISpecialProperties> list)
{
foreach(var item in列表)
{
Console.WriteLine({0} {1},item.Prop1,item.Prop2);
}
}

这似乎很好,但需要处理的列表on不是(也不应该)用接口声明为类型参数。但是,它似乎并不像您可以投射到不同的类型参数。例如,这失败了,我不明白为什么:使用系统;

  
使用System.Collections.Generic;

namespace ConsoleApplication2
{
class Program
{
static void Main(string [] args)
{
List<试验> myList = new List< Test>();
for(int i = 0; i <5; i ++)
{
myList.Add(new Test());

$ b PrintList((IEnumerable< IDoSomething>)myList);
}

static void PrintList(IEnumerable< IDoSomething> list)
{
foreach(IDoSomething item in list)
{
item。做一点事();




interface IDoSomething
{
void DoSomething();

$ b $ public class Test:IDoSomething
{
public void DoSomething()
{
Console.WriteLine(Test did it! );




$ I $ em可以使用 Enumerable.Cast< T> 成员来做到这一点,但我正在寻找一种可能在.NET 2.0中工作的方法。看来这应该是可能的;我错过了什么?

解决方案

问题出在该方法上,而不是如何调用.....

  void PrintProperties< SP>(IEnumerable< SP> list)SP:ISpecialProperties 
{
foreach(var list in list)
{
Console.WriteLine({0} {1},item.Prop1,item.Prop2);
}
}


I'm trying to show someone a use for interfaces in a crazy situation they've created. They have several unrelated objects in lists, and need to perform an operation on two string properties in each object. I'm pointing out that if they define the properties as part of an interface, they can use the interface object as the type for a method parameter that acts on it; for example:

void PrintProperties(IEnumerable<ISpecialProperties> list)
{
    foreach (var item in list)
    {
        Console.WriteLine("{0} {1}", item.Prop1, item.Prop2);
    }
}

This seems like it's all good, but the lists that need to be worked on aren't (and shouldn't) be declared with the interface as the type parameter. However, it doesn't seem like you can cast to a different type parameter. For example, this fails and I can't understand why:

using System;
using System.Collections.Generic;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Test> myList = new List<Test>();
            for (int i = 0; i < 5; i++)
            {
                myList.Add(new Test());
            }

            PrintList((IEnumerable<IDoSomething>)myList);
        }

        static void PrintList(IEnumerable<IDoSomething> list)
        {
            foreach (IDoSomething item in list)
            {
                item.DoSomething();
            }
        }
    }

    interface IDoSomething
    {
        void DoSomething();
    }

    public class Test : IDoSomething
    {
        public void DoSomething()
        {
            Console.WriteLine("Test did it!");
        }
    }
}

I can use the Enumerable.Cast<T> member to do this, but I was looking for a method that might work in .NET 2.0 as well. It seems like this should be possible; what am I missing?

解决方案

The problem is with the method, not with how it's called.....

void PrintProperties<SP>(IEnumerable<SP> list) where SP: ISpecialProperties
{
    foreach (var item in list)
    {
        Console.WriteLine("{0} {1}", item.Prop1, item.Prop2);
    }
}

这篇关于有没有办法将通用列表转换为接口/基类类型列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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