简体   繁体   English

C#中的默认委托

[英]Default delegate in C#

What is the name of the default delegate in C# which takes no parameters and returns void? C#中默认委托的名称是什么,它不带参数并返回void? I remember there existed such a delegate but I don't remember its name. 我记得有一个代表,但我不记得它的名字。

Prior to .NET 3.5, it was fairly common to declare your own. 在.NET 3.5之前,声明自己是相当普遍的。 Now, Action is a good candidate, but ThreadStart was commonly used (fairly confusingly), or MethodInvoker if you were already referencing winforms. 现在, Action是一个很好的候选者,但是ThreadStart是常用的(相当容易混淆),或者如果你已经引用了winforms,则使用MethodInvoker

A quick test (note, running in .NET 4.0, using just some libraries - so not exhaustive): 快速测试(注意,在.NET 4.0中运行,只使用一些库 - 所以不是详尽无遗的):

var qry = from asm in AppDomain.CurrentDomain.GetAssemblies()
          from type in asm.GetTypes()
          where type.IsSubclassOf(typeof(Delegate))
          let method = type.GetMethod("Invoke")
          where method != null && method.ReturnType == typeof(void)
          && method.GetParameters().Length == 0
          orderby type.AssemblyQualifiedName
          select type.AssemblyQualifiedName;
foreach (var name in qry) Console.WriteLine(name);

shows some more candidates: 显示更多候选人:

System.Action, mscorlib...
System.CrossAppDomainDelegate, mscorlib...
System.IO.Pipes.PipeStreamImpersonationWorker, System.Core...
System.Linq.Expressions.Compiler.LambdaCompiler+WriteBack, System.Core...
System.Net.UnlockConnectionDelegate, System...
System.Runtime.Remoting.Contexts.CrossContextDelegate, mscorlib...
System.Threading.ThreadStart, mscorlib...
System.Windows.Forms.AxHost+AboutBoxDelegate, System.Windows.Forms...
System.Windows.Forms.MethodInvoker, System.Windows.Forms...

There are several such delgates, but I think you are looking for Action . 有几个这样的delgates,但我认为你正在寻找Action One other option is MethodInvoker (in System.Windows.Forms). 另一个选项是MethodInvoker (在System.Windows.Forms中)。

You are probably looking for " Action ". 你可能正在寻找“ 行动 ”。

Some related reading: 一些相关阅读:

I may be interpreting your question differently to everyone else, but: 我可能会以不同的方式向您解释您的问题,但是:

If you are thinking of the delegate to use for event handling, convention is to use EventHandler . 如果您正在考虑用于事件处理的委托,则约定是使用EventHandler This delegate doesn't take 'no' parameters, but it is essentially information-less. 该委托不采用“否”参数,但它基本上没有信息。

In .net 2.0 the MethodInvoker delegate is the recomended way. 在.net 2.0中, MethodInvoker委托是推荐的方式。 It is the most generic. 它是最通用的。 As it's name suggest it invokes a method. 正如它的名字所暗示它调用一种方法。 Others might have the same properties you described but they have different name that suggest other usages or very specific usages in specific areas. 其他可能具有您描述的相同属性,但它们具有不同的名称,表明在特定区域中的其他用法或非常具体的用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM