Asa*_*saf 82 c# delegates interface
我需要在班上有一些代表.
我想使用界面"提醒"我设置这些代表.
如何?
我的班级看起来像这样:
public class ClsPictures : myInterface
{
// Implementing the IProcess interface
public event UpdateStatusEventHandler UpdateStatusText;
public delegate void UpdateStatusEventHandler(string Status);
public event StartedEventHandler Started;
public delegate void StartedEventHandler();
}
Run Code Online (Sandbox Code Playgroud)
我需要一个界面来强制这些代表:
public interface myInterface
{
// ?????
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 130
那些是宣布代表类型.它们不属于界面.使用这些委托类型的事件可以在接口中使用:
public delegate void UpdateStatusEventHandler(string status);
public delegate void StartedEventHandler();
public interface IMyInterface
{
event UpdateStatusEventHandler StatusUpdated;
event StartedEventHandler Started;
}
Run Code Online (Sandbox Code Playgroud)
该实现不会(也不应该)重新声明委托类型,而不是重新声明接口中使用的任何其他类型.
DEL*_*zed 28
从.NET 3.5开始,您还可以使用System.Action委托,这将产生以下接口:
public interface myInterface
{
// Implementing the IProcess interface
event Action<String> UpdateStatusText;
event Action Started;
}
Run Code Online (Sandbox Code Playgroud)
小智 8
只需将委托作为属性公开
public delegate void UpdateStatusEventHandler(string status);
public delegate void StartedEventHandler();
public interface IMyInterface
{
UpdateStatusEventHandler StatusUpdated {get; set;}
StartedEventHandler Started {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
Jon Skeet的回答是对的,我只想添加一个注释.
界面不是为了"提醒"你做什么,或者在你的课程中包括什么.接口是抽象的手段,用于面向对象的编程和设计方法.也许你根本不需要接口声明,除非你想看到一些具体的类实例作为程序中其他地方的接口(Abstraction).
如果要在项目中强制执行某些编码标准,可能需要尝试使用代码分析工具(如在Visual Studio中) - 它们允许扩展,您可以合并添加自己的代码分析规则.
使用代码分析,如果你"忘记"添加代理(虽然我没有看到忘记它的点,好像没有使用委托,也不需要),你会收到警告/错误.
| 归档时间: |
|
| 查看次数: |
77403 次 |
| 最近记录: |