如何将委托添加到一个界面C# [英] How to add a delegate to an interface C#

查看:243
本文介绍了如何将委托添加到一个界面C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我班上一些代表。



我想使用的接口为提醒我来设置这些代表。



如何



我的阶级是这样的:

 公共类ClsPictures:MyInterface的
{
//实施IProcess接口
公共事件UpdateStatusEventHandler UpdateStatusText;
公众委托无效UpdateStatusEventHandler(字符串状态);

公共事件StartedEventHandler启动;
公众委托无效StartedEventHandler();
}



我需要一个接口来迫使这些代表:

 公共接口MyInterface的
{
// ?????
}


解决方案

那些正在申报委托的类型的。他们没有在接口属于。事件使用的这些委托类型的罚款是在虽然接口:

 公共委托无效UpdateStatusEventHandler (字符串地位); 
公众委托无效StartedEventHandler();

公共接口IMyInterface的
{
事件UpdateStatusEventHandler StatusUpdated;
事件StartedEventHandler启动;
}

的实施不会(也不应该)重新声明委托类型,任何超过它将重新声明中的接口中使用的任何其它类型的


I need to have some delegates in my class.

I'd like to use the interface to "remind" me to set these delegates.

How to?

My class look like this:

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();
}

I need an interface to force those delegates:

public interface myInterface
{
   // ?????
}

解决方案

Those are declaring delegate types. They don't belong in an interface. The events using those delegate types are fine to be in the interface though:

public delegate void UpdateStatusEventHandler(string status);
public delegate void StartedEventHandler();

public interface IMyInterface
{       
    event UpdateStatusEventHandler StatusUpdated;    
    event StartedEventHandler Started;
}

The implementation won't (and shouldn't) redeclare the delegate type, any more than it would redeclare any other type used in an interface.

这篇关于如何将委托添加到一个界面C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆