通过引用传递:多个接口的子节点

Tho*_*ews 2 c# multiple-inheritance pass-by-reference

将实现多个接口的对象传递给只需要其中一个接口的函数时,我遇到了构建错误.

我正在开发一个通讯包.该软件包有一个Receiver类和一个Sender类.Receiver类使用通知接口Receive_Notifier(函数对象)来处理通知.同样,Sender类使用通知接口Send_Notifier来处理通知.

interface Send_Notifier
{
    void sending_text(string text);
}

interface Receive_Notifier
{
    void raw_text_received(string raw_text);
}

class Sender
{
    Send_Notifier m_notifier = null;
    public Sender(ref Send_Notifier notifier)
    {
        m_notifier = notifier;
    }
    public void send(string text)
    {
        m_notifier.sending_text(text);
        return;
    }
}

class Receiver
{
    Receive_Notifier m_notifier = null;
    public Sender(ref Receive_Notifier notifier)
    {
        m_notifier = notifier;
    }
    public void receive(string text)
    {
        m_notifier.raw_text_received(text);
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)

我已将接口组合到Communications_Notifier中:

interface Communications_Notifier
    : Send_Notifier, Receive_Notifier
{
}
Run Code Online (Sandbox Code Playgroud)

我创建了一个Notifier实现Communications_Notifier接口的类:

class Notifier
    : Communications_Notifier
{
    public void sending_text(string text)
    {
        System.Console.WriteLine("--> " + text);
    }
    public void raw_text_received(string raw_text)
    {
        System.Console.WriteLine("<-- " + raw_text);
    }
}
Run Code Online (Sandbox Code Playgroud)

为了简化这篇文章,我只展示发件人类:

class Sender
{
    Send_Notifier m_notifier = null;
    public Sender(ref Send_Notifier notifier)
    {
        m_notifier = notifier;
    }
    public void send(string text)
    {
        m_notifier.sending_text(text);
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是当我将一个实例传递NotifierSender该类的实例时:

class Program
{
    static void Main(string[] args)
    {
        Notifier the_notifier = new Notifier();
        Sender talker = new Sender(ref the_notifier); // ** Error generating line

        talker.send("Hello\n");

        string pause_text;
        pause_text = System.Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

Visual C#Express 2010中的错误:

Error   1   The best overloaded method match for 'Multiple_Inheritance_Interface.Sender.Sender(ref Multiple_Inheritance_Interface.Send_Notifier)' has some invalid arguments    C:\Users\Thomas\Programming_Experiments\C_Sharp\Multiple_Inheritance_Interface\Multiple_Inheritance_Interface\Program.cs    55  29  Multiple_Inheritance_Interface  
Error   2   Argument '1': cannot convert from 'ref Multiple_Inheritance_Interface.Notifier' to 'ref Multiple_Inheritance_Interface.Send_Notifier'   C:\Users\Thomas\Programming_Experiments\C_Sharp\Multiple_Inheritance_Interface\Multiple_Inheritance_Interface\Program.cs    55  44  Multiple_Inheritance_Interface
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 为什么不能将Notifier其转换为类型的引用 Sender,因为它实现了 Sender接口?
  2. 为什么构造函数的参数无效?

注意:我正在从C++,C和Java背景过渡到C#.

Cod*_*aos 6

ref参数不支持共方差或反方差.这是必要的,因为它可以读取和更改引用.因此,如果您传入一个派生类,并且该函数分配了一个不属于该派生类型的新实例,那么您的代码就会中断.

例:

void MakeCat(ref Animal animal)
{
  animal=new Cat();
}

Dog dog=new Dog();
MakeCat(ref dog);
Run Code Online (Sandbox Code Playgroud)

这显然不起作用,因为现在你Catdog变量中有一个.


我不确定你为什么要使用ref它.使用引用类型,您已经可以更改传入的实例的内容.您无法使用该引用类型的新实例替换传入的变量.


out另一方面,参数看起来像是协变的.我怀疑它们不是由于运行时的限制:它们实际上是ref标记有属性的参数,告诉编译器将其视为out参数.