相关疑难解决方法(0)

为参数指定多个接口

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

public interface IObject
{
    string Name { get; }
    string Class { get; }
    IEnumerable<IObjectProperty> Properties { get; }
}
public interface ITreeNode<T>
{
    T Parent { get; }
    IEnumerable<T> Children { get; }
}
Run Code Online (Sandbox Code Playgroud)

这样的

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; …
Run Code Online (Sandbox Code Playgroud)

c# interface composition

21
推荐指数
2
解决办法
7920
查看次数

有没有办法声明一个在.Net中实现多个接口的变量?

此Java问题类似.我想指定一个变量实现多个接口.例如

private {IFirstInterface, ISecondInterface} _foo;

public void SetFoo({IFirstInterface, ISecondInterface} value)
{
    _foo = value;
}
Run Code Online (Sandbox Code Playgroud)

要求:

  • 我无法为大多数传入Foo的类型添加接口.所以我无法创建继承自IFirstInterface和ISecondInterface的第三个接口.
  • 如果可能的话,我想避免使包含类变得通用,因为Foo的类型与类没有太大关系,并且用户在编译时不太可能知道它.
  • 我需要在以后使用foo来访问两个接口中的方法.
  • 我想以编译器安全的方式执行此操作,即在尝试使用它之前不尝试强制转换为接口.如果foo没有实现两个接口,相当多的功能将无法正常工作.

这可能吗?

编辑:出于各种原因,我想要这几次.在这个例子中,因为我正在将一组属性从ObservableCollections更改为ReadOnlyObservableCollections.我有一个帮助器类,它创建从源ObservableCollection到另一个Collection的投影.由于ReadOnlyObservableCollection不从ObservableCollection继承而且我只需要IList和INotifyCollectionChanged中的操作,我希望将我的源集合存储为这两个接口的组合,而不是需要ObservableCollection.

.net interface

18
推荐指数
2
解决办法
4622
查看次数

如何使用Type Constraint创建实现两个接口的Generic Collection?

对不起我在c#中没有处理泛型

根据这个问题,如何制作一个实现两个接口的generc集合我正在寻找这样的直接方式:当然它会产生错误并且完全是错误的.

interface IEmployee {void DisplayInfo();}

interface ISalary {void CalculateSalary();}


class Nurse : IEmployee, ISalary
{
 //some Implementation
}


class Doctor : IEmployee, ISalary
{
 //some Implementation
}

class EntryPoint
{
 static void Main(string[] args)
  { 
  System.Collections.Generic .List<T>  employees where T: ISalary,IEmployee
   =new System.Collections.Generic .List<T>();
  }

 Nurse oNurse = new Nurse();
 Doctor oDoctor = new Doctor();

 employees.Add(oNurse);
 employees.Add(oDoctor);
}
Run Code Online (Sandbox Code Playgroud)

经过一些阅读后,我发现可能我必须首先定义这样的泛型类:

public class HospitalEmployee<T> where T : IEmployee, ISalary
Run Code Online (Sandbox Code Playgroud)

{

}

不幸的是,它很有用,现在我很困惑,不知道该怎么做,请帮助,谢谢你

c# generics syntax

1
推荐指数
1
解决办法
1381
查看次数

C#使用Interface1和Interface2接受对象

重复:

是否可以使参数实现两个接口?


寻找一种方法来做到这一点:

public interface INumber1 {}
public interface INumber2 {}
public class TestObject : INumber1, INumber2 {}

public static class TestClass
{
    public static void TestMethod(INumber1&INumber2 testobject)
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

调用TestMethod:

TestClass.TestMethod(new TestObject());
Run Code Online (Sandbox Code Playgroud)

问题是我需要一个参数来包含来自INumber1和INumber2的方法.提示?

c# interface

0
推荐指数
1
解决办法
212
查看次数

标签 统计

c# ×3

interface ×3

.net ×1

composition ×1

generics ×1

syntax ×1