开放泛型类型的ObservableCollection [英] ObservableCollection of open generic type

查看:120
本文介绍了开放泛型类型的ObservableCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口

  public interface IImageFilter< TIn,TOut> 
{
//属性
TIn输入{get;组; }
string Name {get;组; }
Guid Guid {get;组; }

进程进程(TIn帧);
}

我需要一个可观察的实现接口的对象集合。

  private ObservableCollection< IImageFilter< T,U>> _imageFilters; 

传递给集合的对象可以是

  IImageFilter< string,string> 
IImageFilter< string,int>
IImageFilter< int,double>

它如何声明_imageFilters? T是什么?或U?

解决方案

不是不可能的,另一种方法是使用Covariant通用类型。但您的界面需要更改。



您的界面:

 内部接口IImageFilter< out I,out O> 
{
I Input {get; }

O Process();
}

接口实现

  public class ImageFilter:IImageFilter< string,string> 
{
public string Input {get;私人设置; }

public ImageFilter(string input)
{
Input = input;
}

public string Process()
{
return Input.ToUpper();


$ / code $ / pre

用法:

  List< IImageFilter< object,object>> filters = new List< IImageFilter< object,object>>(); 
ImageFilter filter = new ImageFilter(something);
filters.Add(filter);


I have an interface

public interface IImageFilter<TIn, TOut>
{
    // Properties
    TIn Input { get; set; }
    string Name { get; set; }
    Guid Guid { get; set; }

    TOut Process(TIn frame);
}

and I needed an observable collection of objects that implement the interface.

private ObservableCollection<IImageFilter<T, U>> _imageFilters;

the object that i pass to the collection can be

IImageFilter<string, string>
IImageFilter<string, int>
IImageFilter<int, double>

How do it declare the _imageFilters? what's the T? or U?

解决方案

Not really impossible, Another approach is to use Covariant Generic type. But it will require some change in your interface.

Your Interface:

internal interface IImageFilter<out I, out O>
{
    I Input { get; }

    O Process();
}

Interface Implementation

public class ImageFilter : IImageFilter<string, string>
{
    public string Input { get; private set; }

    public ImageFilter(string input)
    {
        Input = input;
    }

    public string Process()
    {
        return Input.ToUpper();
    }
}

Usage:

   List<IImageFilter<object, object>> filters= new List<IImageFilter<object, object>>();
   ImageFilter filter= new ImageFilter("something");
   filters.Add(filter);

这篇关于开放泛型类型的ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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