Converting inerface with delegate from vb to c#

南楼画角 提交于 2020-01-05 08:09:21

问题


I've got interface:

Public Interface ICSIItem

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

End Interface

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

and resharper says me that delegate cannot be in interface.

This is NotebookAnswerChangedEventArgs class

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

    public string PreviousAnswer;

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

Could you help with this converting and please for better converter site, because that is very poor :/


回答1:


C#, unlike VB.Net, doesn't allow an interface to declare nested types. This includes delegates. To fix this simply move the AnswerValueChangedEventHandler delegate outside the interface .

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

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



回答2:


public interface ICSIItem {

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

}

please note - your eventargs supports the use of EventHandler - I think in such cases you should use the EventHandler instead of defining your own delegates anew.

but I don't know any good converters. Why do you convert after all? Just put your VB.net code in a dll and use it from C# - if you write new one do it in C# and convert with time as the need arises.



来源:https://stackoverflow.com/questions/7201587/converting-inerface-with-delegate-from-vb-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!