在C#中添加类型过滤器 [英] Adding Type Filters In C#

查看:92
本文介绍了在C#中添加类型过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何向.NET Dictionary添加类型过滤器,以便TValue成为继承EventListener接口的类对象?所以例如



How do I add type filters to a .NET Dictionary so that the TValue will be a class object that inherits my EventListener interface? So for example

Dictionary<string, object> events = new Dictionary<string, object>(); //where object would have to inherit EventListener

推荐答案

查看以下示例:

Look at following sample:
interface I {};

class a : I {};
class b : I {};
class c {};

void Main()
{
    Dictionary<string, I> x = new Dictionary<string, I>();
    x.Add("a", new a());
    x.Add("b", new b());
    x.Add("c", new c()); //will fail
}


这篇关于在C#中添加类型过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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