C#:标志着一个方法强制执行的方式是那里接口的一部分 [英] C#: enforceable way of signifying a method is there as part of interface

查看:125
本文介绍了C#:标志着一个方法强制执行的方式是那里接口的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在C#的方式来标记的方法作为一个类的一部分来满足这个类实现的接口?我发现自己想知道,有时在一个类中的code挖的时候,为什么有些方法是有,但,当我尝试删除之一,因为它不使用,我看这是必要的,以便为类来实现一些接口。它会是不错的这些方法标记。有点像<一个href=\"http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why\"><$c$c>@Override在Java注释,也许吧。

Is there a way in C# to mark a method as being part of a class to satisfy an interface that the class implements? I find myself wondering sometimes when digging in a class's code why some methods are there but then, when I try to remove one since it isn't in use, I see it's necessary in order for the class to implement some interface. It'd be nice to have these methods marked as such. Something like the @Override annotation in Java, maybe.

编辑:的我想preFER到的的显式实现该接口,因为再访问我的类的实例接口方法变得更麻烦(即,有投 MyClass的 IMyInterface的之前调用 MyInterfaceMethod )。

I would prefer to not explicitly implement the interface because then accessing the interface methods on an instance of my class becomes more of a hassle (i.e., having to cast an instance of MyClass to be IMyInterface before calling MyInterfaceMethod).

编辑:的我也要preFER不使用的区域。我不想松散描述通过一些任意名称code的大块作为是一个接口实现的一部分,而是指特定的方法,无论他们在哪里是在类中,因为作为一个接口的一部分。即使是公司拟说法,其中接口中的方法属于将是不错的一些XML注释模板。

I would also prefer not to use regions. I don't want a big block of code described loosely through some arbitrary name as being part of an interface implementation, but rather to denote specific methods, wherever they may be within the class, as being part of an interface. Even some XML comment template that's intended for saying which interface the method belongs to would be nice.

编辑:的一切答案似乎表明我明确地实现接口,这是我不想做的,或者说,我使用的区域,这也是我不想做的事。我认为,<一个href=\"http://stackoverflow.com/questions/2164159/c-signify-a-method-is-there-as-part-of-interface/2164181#2164181\">Will了解我的要求最好的。我希望这样的事情注解,所以我可以做类似以下内容:

All the answers seem to suggest I explicitly implement interfaces, which I don't want to do, or that I use regions, which I also don't want to do. I think Will understood my request best. I was hoping for something like an annotation, so I could do something like the following:

[Interface(IMyInterface)]
public void MyImplicitlyImplementedInterfaceMethod() { }

或者,如dss539在评论中提及到<一个href=\"http://stackoverflow.com/questions/2164159/c-signify-a-method-is-there-as-part-of-interface/2164214#2164214\">this回答:

public implement void MyImplicitlyImplementedInterfaceMethod() { }

就像我们有覆盖今天:公共覆盖布尔等于(obj对象){}

推荐答案

您有三个选项:

明确实现的接口:

您可以明确实现的接口使用语法 IInterface.MethodName ,例如:

You can explicitly implement the interface using the syntax IInterface.MethodName, for example:

bool IInterface.MethodName(string other) 
{
    // code
}

在一个区域裹接口成员

工具都已经换你code在一个地区,如果你选择实现接口,通过用户界面:

Tools will already wrap your code in a region if you choose to "Implement an interface" through the UI:

#region IInterface members

public bool MethodName(string other)
{
    // code
}

#endregion

通过注释文档

C#,您可以通过 // 添加多个种类的意见, / * XML文档评论。使用从优!

C# allows you to add multiple kinds of comments through //, /*, or XML documentation comments. Use liberally!

这篇关于C#:标志着一个方法强制执行的方式是那里接口的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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