将通用对象列表传递给方法 [英] Pass generic List of objects to a method

查看:72
本文介绍了将通用对象列表传递给方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口和一组类.

I have an interface and a set of classes.

推荐答案

看看 泛型.关键是您对接口的正确声明:

Take a look at Generics again. The key is the correct declaration of you interface:

public interface ICommand<T>
{
    void Execute<T>(List<T> data);
    void UnExecute<T>(List<T> data);
}

因此,您的具体实现是:

So your concrete implementations are:

public class AddCommand : ICommand<Data> {}
public class AnotherCommand : ICommand<AnotherData> {}

在大多数情况下,派生所有 来自通用基类或接口的DTO ,可在所有命令中提供基本功能.在这种情况下,它看起来像:

In most cases it is useful to derive all your DTO's from a common base class or interface to provide a base functionality in all your commands. In this case it would look like:

interface IData 
{
   string Source;
}

interface ICommand<T>
  where T : IData
{
    void Execute<T>(List<T> data);
    void UnExecute<T>(List<T> data);
}

class SomeData : IData {}
class MoreData : IData {}

class SomeCommand : ICommand<SomeData > {}
class MoreCommand : ICommand<MoreData > {}




这篇关于将通用对象列表传递给方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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