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

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; }
}

现在,我有了一个需要其参数之一才能实现这两个接口(interface)的函数。我将如何在C#中进行指定?

一个例子是
public TypedObject(ITreeNode<IObject> baseObject, IEnumerable<IType> types, ITreeNode<IObject>, IObject parent)
{
    //Construct here
}

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

最佳答案

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

关于c# - 为参数指定多个接口(interface),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4073440/

10-11 06:43