C# 我什么时候会在 asp.net 中使用委托?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/635015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:10:20  来源:igfitidea点击:

When would I use a delegate in asp.net?

c#asp.netdelegates

提问by johnny

I'm always looking for a way to use all the tools I can and to stretch myself just beyond where I am at. But as much as I have read about delegates, I can never find a place to use them (like Interfaces, Generics, and a lot of stuff, but I digress.) I was hoping someone could show me when and how they used a delegate in webprogramming for asp.net c#(2.0 and above).

我一直在寻找一种方法来使用我所能使用的所有工具,并让自己超越我所处的位置。但就我读过的关于委托的内容而言,我永远找不到使用它们的地方(比如接口、泛型和很多东西,但我离题了。)我希望有人能告诉我他们何时以及如何使用委托在asp.net c#(2.0 及更高版本)的web编程中。

Thank you and if this wrong for Stack Overflow, please just let me know.

谢谢,如果 Stack Overflow 有问题,请告诉我。

采纳答案by Boydski

bdukes is right about events. But you're not limited to just using delegates with events.

bdukes 对事件的看法是正确的。但是您不仅限于将委托与事件一起使用。

Study the classic Observer Pattern for more examples on using delegates. Some text on the pattern points toward an event model, but from a raw learning perspective, you don't have to use events.

研究经典的观察者模式以获取更多关于使用委托的例子。模式上的一些文本指向事件模型,但从原始学习的角度来看,您不必使用事件。

One thing to remember: A delegate is just another type that can be used & passed around similar to your primitive types such as an "int". And just like "int", a delegate has it's own special characteristics that you can act on in your coding when you consume the delegate type.

要记住的一件事:委托只是另一种可以使用和传递的类型,类似于您的原始类型,例如“int”。就像“int”一样,委托有它自己的特殊特征,当您使用委托类型时,您可以在编码中对其进行操作。

To get a really great handle on the subject and on some of it's more advanced and detailed aspects, get Joe Duffy's book, .NET Framework 2.0.

要真正很好地处理该主题以及其中一些更高级和更详细的方面,请阅读 Joe Duffy 的书.NET Framework 2.0

回答by bdukes

Well, whenever you handle an event, you're using a delegate.

好吧,无论何时处理事件,都在使用委托。

回答by eglasius

There isn't anything special to asp.net related to delegates (besides considerations when using async stuff, which is a whole different question), so I will point you to other questions instead:

与委托相关的 asp.net 没有任何特别之处(除了使用异步内容时的注意事项,这是一个完全不同的问题),因此我将向您指出其他问题:

Delegate Usage : Business Applications

委托使用:业务应用程序

Where do I use delegates?

我在哪里使用委托?

回答by Walden Leverich

To answer your second question first, I think this is a great question for StackOverflow!

首先回答你的第二个问题,我认为这对 StackOverflow 来说是一个很好的问题!

On the first, one example would be sorting. The Sort() method on List takes a delegate to do the sorting, as does the Find() method. I'm not a huge fan of sorting in the database, so I like to use Sort() on my result sets. After all, the order of a list is much more of a UI issue (typically) than a business rule issue.

第一个例子是排序。List 上的 Sort() 方法采用委托进行排序,Find() 方法也是如此。我不太喜欢在数据库中排序,所以我喜欢在我的结果集上使用 Sort()。毕竟,列表的顺序更像是 UI 问题(通常)而不是业务规则问题。

Edit: I've added my reasons for sorting outside the DB to the appropriate question here.

编辑:我已将我在数据库外排序的原因添加到此处的相应问题中。

Edit: The comparison function used in the sort routine is a delegate. Therefore, if you sort a List using the .Sort(Comparison(T)) method the Comparison(T) method you pass to the sort function is a delegate. See the .Sort(Comparison(T)) documentation.

编辑:排序例程中使用的比较函数是一个委托。因此,如果您使用 .Sort(Comparison(T)) 方法对 List 进行排序,则传递给排序函数的比较(T) 方法是一个委托。请参阅 .Sort(Comparison(T))文档

回答by Kelsey

Another example would be to publish events for user controls.

另一个示例是发布用户控件的事件。

Eg.

例如。

// In your user control
public delegate void evtSomething(SomeData oYourData);
public event evtSomething OnSomething;

// In the page using your user control
ucYourUserControl.OnSomething += ucYourUserControl_OnSomething;

// Then implement the function
protected void ucYourUserControl_OnSelect(SomeData oYourData)
{
   ...
}

回答by Bryan Rowe

Another quick example off the top of my head would be unit testing with Rhino Mocks. A lot of the things you can do with Rhino Mocks utilize delegates and lambda expressions.

我脑海中的另一个快速示例是使用 Rhino Mocks 进行单元测试。你可以用 Rhino Mocks 做的很多事情都利用了委托和 lambda 表达式。

回答by Jhonny D. Cano -Leftware-

Recently i used the delegates for "delegating" the checking of the permissions.

最近我使用委托来“委托”检查权限。

public Func CheckPermission;

公共功能检查权限;

This way, the CheckPermission function can be shared by various controls or classes, say it in a static class or a utilities class, and still be managed centralized, avoiding also Interface explossion; just a thought

这样,CheckPermission 函数可以被各种控件或类共享,比如在静态类或实用程序类中,仍然集中管理,也避免了接口爆炸;只是一个想法

回答by Helen Toomik

You can use delegates whenever you know you will want to take some action, but the details of that action will depend on circumstances.

只要您知道要采取某些行动,就可以使用委托,但该行动的详细信息将取决于具体情况。

Among other things, we use delegates for:

除其他外,我们将委托用于:

  • Sorting and filtering, especially if the user can choose between different sorting/filtering criteria
  • Simplifying code. For example, a longish process where the beginning and end are always the same, but a small middle bit varies. Instead of having a hard-to-read if block in the middle, I have one method for the whole process, and pass in a delegate (Action) for the middle bit.
  • I have a very useful ToString method in my presentation layer which converts a collection of anything into a comma-separated list. The method parameters are an IEnumerable and a Func delegate for turning each T in the collection into a string. It works equally well for stringing together Users by their FirstName or for listing Projects by their ID.
  • 排序和过滤,特别是如果用户可以在不同的排序/过滤标准之间进行选择
  • 简化代码。例如,一个较长的过程,开始和结束总是相同的,但中间的一个小位不同。而不是中间有一个难以阅读的 if 块,我有整个过程的一种方法,并为中间位传递一个委托(Action)。
  • 我的表示层中有一个非常有用的 ToString 方法,它可以将任何内容的集合转换为逗号分隔的列表。方法参数是一个 IEnumerable 和一个 Func 委托,用于将集合中的每个 T 转换为字符串。它同样适用于通过名字将用户串在一起或通过他们的 ID 列出项目。