为一个参数指定多个接口 [英] Specifying multiple interfaces for a parameter

查看:86
本文介绍了为一个参数指定多个接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现两个接口的对象...接口是:

I have an object that implements two interfaces... The interfaces are:

public interface IObject
{
    string Name { get; }
    string Class { get; }
    IEnumerable<IObjectProperty> Properties { get; }
}
public interface ITreeNode<T>
{
    T Parent { get; }
    IEnumerable<T> Children { get; }
}

使得

public class ObjectNode : IObject, ITreeNode<IObject>
{
    public string Class { get; private set; }
    public string Name { get; private set; }
    public IEnumerable<IObjectProperty> Properties { get; set; }
    public IEnumerable<IObject> Children { get; private set; }
    public IObject Parent { get; private set; }
}

现在,我有一个函数,需要使用其参数之一来实现这些接口。我将如何在C#中指定它?

Now i have a function which needs one of its parameters to implement both of these interfaces. How would i go about specifying that in C#?

示例为

public TypedObject(ITreeNode<IObject> baseObject, IEnumerable<IType> types, ITreeNode<IObject>, IObject parent)
{
    //Construct here
}

还是我的设计错误,我应该以某种方式在一个接口上实现这两个接口的问题

Or is the problem that my design is wrong and i should be implementing both those interfaces on one interface somehow

推荐答案

public void Foo<T>(T myParam)
    where T : IObject, ITreeNode<IObject>
{
    // whatever
}

这篇关于为一个参数指定多个接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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