将带有委托的接口从vb转换为c#

时间:2011-08-26 07:53:24

标签: c# vb.net interface vb.net-to-c#

我有界面:

Public Interface ICSIItem

    Sub Initialize()
    Event AnswerValueChanged(ByVal sender As Object, ByVal e As NotebookAnswerChangedEventArgs)
    Property DataContext() As Object

End Interface

和转换器http://www.developerfusion.com/tools/convert/vb-to-csharp/

public interface ICSIItem
{
    void Initialize();
    event AnswerValueChangedEventHandler AnswerValueChanged;
    delegate void AnswerValueChangedEventHandler(object sender, NotebookAnswerChangedEventArgs e);
    object DataContext { get; set; }
}

并且resharper告诉我,委托不能在界面中。

这是NotebookAnswerChangedEventArgs类

public class NotebookAnswerChangedEventArgs : System.EventArgs
{
    public string NewAnswer;

    public string PreviousAnswer;

    public NotebookAnswerChangedEventArgs(string newAnswer, string previousAnswer) : base()
    {
        this.NewAnswer = newAnswer;
        this.PreviousAnswer = previousAnswer;
    }
}

你能帮忙进行这种转换,并请更好的转换器网站,因为这是非常差的:/

2 个答案:

答案 0 :(得分:2)

与VB.Net不同,C#不允许接口声明嵌套类型。这包括代表。要解决此问题,只需将AnswerValueChangedEventHandler委托移到界面外。

public delegate void AnswerValueChangedEventHandler(
  object sender, 
  NotebookAnswerChangedEventArgs e);

public interface ICSIItem {
  void Initialize();
  event AnswerValueChangedEventHandler AnswerValueChanged;
  object DataContext { get; set; }
}

答案 1 :(得分:1)

public interface ICSIItem {

   void Initialize();
   event EventHandler<NotebookAnswerChangedEventArgs> AnswerValueChanged;
   object DataContext { get; set; }

}

请注意 - 您的eventargs支持使用EventHandler - 我认为在这种情况下您应该使用EventHandler而不是重新定义自己的委托。

但我不知道有什么好的转换器。你为什么要转换?只需将您的VB.net代码放入dll并从C#中使用它 - 如果您编写新代码,请在C#中执行,并在需要时随时间转换。