【问题标题】:c# call method in other classc#调用其他类中的方法
【发布时间】:2013-07-28 13:17:03
【问题描述】:

我对 c# 比较陌生。我在底部有一个有 sender.send() 命令的类。我需要从头等舱调用它,并将我创建的“出站”传递给它,以便它会执行 sender.send 位 - 请帮助。 说明 ISender 是一个接口,Handler 和 Publisher 是类。

class CVHandler : Handler<CVRequest>
{
    protected override object Do(CVRequest Body, object previousResult)
    {   Message outbound = new Message(Body);
        outbound.Key = "PM";

        //Send the message

        return null;
    }
}

public class CVPublisher : Publisher
{
    protected override void Do(ISender sender)
    {
        sender.Send(message);
    }
}

【问题讨论】:

  • 你需要 Do 方法 Internal 或 public 才能从第一类访问它。

标签: c# class interface


【解决方案1】:

当定义一个方法为protected时,它只能被这个类及其子类使用。这就是为什么你不能从CVHandler 调用它的原因。

【讨论】:

  • 嗨,好的,这让我可以访问它。谢谢。然后我得到一个问题,如果我尝试这个 CVPublisher send = new CVPublisher();发送.Do(消息);它告诉我它的预期类型是 ISender - 而不是类型 Message 这是我构建为我的“出站”
  • 嗨,我建议您使用新代码编辑您的问题,或者更好的是,打开另一个带有相关代码和确切错误的问题。很难将其作为评论阅读。
猜你喜欢
  • 2017-06-25
  • 2012-04-03
  • 1970-01-01
  • 2012-11-05
  • 2021-05-29
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多