简体   繁体   English

如何定义一个接口方法,其不同的实现可以返回<t> , 列表<t> , 列表<list<t> &gt; 其中 T 是自定义 class </list<t></t></t>

[英]How can I define an interface method whose different implementation can return <T>, List<T>, List<List<T>> where T is a custom class

interface IParser
{
     return type parseData(list<byte> data);
}

Where return type can be a T , List<T> or List<List<T>>返回类型可以是TList<T>List<List<T>>

Declare your interface like this像这样声明你的界面

interface IParser<T>
{
    T ParseData(List<byte> data);
}

Can be used like this:可以这样使用:

public class Parser<T> : IParser<T>
{
    public virtual T ParseData(List<byte> data) //virtual is optional
    {
        //do stuff and return T
        //T could be anything, a List, a struct, a class what ever
        return default(T);
    }
}

If you need a specific parser you could do this:如果你需要一个特定的解析器,你可以这样做:

public class StringParser : Parser<string>
{
    public override string ParseData(List<byte> data)
    {
        return string.Empty;
    }
}

Or if you need a specific parser for a collection of T或者,如果您需要特定的解析器来处理T的集合

public class CollectionParser<TItem> : Parser<IEnumerable<TItem>>
{
    public override IEnumerable<TItem> ParseData(List<byte> data)
    {
        return Enumerable.Empty<TItem>;
    }
}

Something like this:像这样的东西:

public interface IParser<TOut> where TOut : class
{
    TOut ParseData(List<byte> data);
}

edit编辑

Since the question was about T as a custom class thereby verify that the type of TOut is class.由于问题是关于 T 作为自定义 class 从而验证 TOut 的类型是 class。 Maybe a good addition would be as following:也许一个很好的补充如下:

public interface IParser<in TIn, out TOut> where TOut : class
{
    TOut ParseData(TIn data);
}

and the implementation would be:实施将是:

class Parser : IParser<List<byte>, CustomClass>
{
    CustomClass ParseData(List<byte> data)
    {
     return new CustomClass();
    }
}

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

相关问题 如何创建一个 List(of T),其中 T 是 (T) 的接口,List 的内容继承了一个实现 T 接口的抽象类 - How can I create a List(of T) where T is an interface of (T) and the contents of the List inherit an Abstract class that implements the interface of T 如何定义可以返回DataTable或List的方法 <T> ? - How to define a method that can return a DataTable or List <T>? 为什么我不能声明通用列表 <T> 作为接口方法的返回类型? - Why can't I declare generic List<T> as return type for interface method? 如何在自定义列表上调用方法<T>从属性? - How can I call method on custom List<T> from a property? 我如何串联两个不同的列表 <T> 两者都有相同的基类? - How can i concatenate two different list<T> where both have same base class? 通用方法,其中 T 是实现接口的列表 - Generic Method where T is List that implements interface 我在哪里可以查找清单 <T> .AddRange()方法? - Where Can I Find List<T>.AddRange() Method? 我怎样才能安全返回List <T> 从声明为IEnumerable的方法/属性 <T> ? - How can I safely return List<T> from method/property declared as IEnumerable<T>? 我如何返回通用列表 <T> 从通用方法 <T> () - How can i return a generic List<T> from a generic method<T>() 我可以创建一个列表<class<t> &gt;? </class<t> - Can I create a List<Class<T>>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM