草庐IT

Delegate

全部标签

c# - 委托(delegate)、Lambda 和 LINQ,我的天哪!

作为一名相当初级的开发人员,我遇到了一个问题,它凸显了我缺乏经验和知识上的漏洞。如果这里的序言太长,请原谅。我发现自己在一个项目中需要学习许多(对我来说)新技术,包括LINQ(为了这个项目的目的,到OBJECTS和XML)等等。到目前为止我读到的所有内容都表明,要使用LINQ,我需要完全理解以下内容(委托(delegate)、匿名方法和Lambda表达式)。好的,现在有趣的来了。我过去使用.NET事件模型时使用过委托(delegate),但大部分细节对我来说是隐藏的(感谢Microsoft!)。我知道在基本层面上,委托(delegate)实例是指向方法的指针(我知道这是一种严重的过度简

c# - 分配给委托(delegate)的通用方法

我对委托(delegate)和泛型方法有点困惑。是否可以将委托(delegate)分配给具有泛型类型参数的方法?即://Thisdoesn'tallowmetopassagenericparameterwiththedelegate.publicdelegatevoidGenericDelegate()someDelegate=GenericMethod;publicvoidGenericMethod()whereT:ISomeClass{}我正在尝试将此委托(delegate)传递给具有该方法所期望的通用接口(interface)类型的函数,函数如下:voidCheckDelegat

c# - 如何修复 'compiler error - cannot convert from method group to System.Delegate' ?

publicMainWindow(){CommandManager.AddExecutedHandler(this,ExecuteHandler);}voidExecuteHandler(objectsender,ExecutedRoutedEventArgse){}错误1​​参数2:无法从“方法组”转换为“System.Delegate” 最佳答案 我猜有多个具有不同签名的ExecuteHandler。只需将您的处理程序转换为您想要的版本:CommandManager.AddExecuteHandler(this,(Action)

c# - 将通用 Action<T> 委托(delegate)添加到列表

是否可以将通用委托(delegate)操作添加到列表集合中?我需要某种用于Silverlight应用程序的简单消息传递系统。更新以下是我真正“想要”的classSomeClass{publicTData{get;set;}//andmore....}classApp{List>>_actions=newList>>();voidAdd(Action>foo){_actions.Add(foo);}}编译器:Thetypeornamespacename'T'couldnotbefound(areyoumissingausingdirectiveoranassemblyreference?

c# - Func 委托(delegate)与函数

谁能告诉我使用委托(delegate)而不是调用函数本身的优势,如下所示(或者换句话说,为什么选择选项A而不是选项B)?昨晚我在看某人的linq代码,他们有类似于选项A的东西,但它被用来返回编译的linq查询。我意识到前者现在可以传递给其他功能..只是不确定它的实用性。顺便说一句,我意识到这不会按原样编译。在发布之前取消注释其中一个函数。蒂亚classProgram{staticvoidMain(string[]args){Console.WriteLine(SayTwoWords("Hello","World"));Console.ReadKey();}//OptionAprivat

c# - 匿名委托(delegate)中捕获的私有(private)字段

classA{publiceventEventHandlerAEvent;}classB{privateA_foo;privateint_bar;publicvoidAttachToAEvent(){_foo.AEvent+=delegate(){...UseBar(_bar);...}}}由于delegate捕获变量this._bar,它是否隐含地持有B的实例?B的实例是否会通过事件处理程序引用并由A的实例捕获变量?如果_bar是AttachToAEvent方法的局部变量,会有不同吗?因为在我的例子中,A的实例比B的实例生命周期长得多并且小得多,我担心会导致“内存泄漏”这样做。

c# - 帮助理解 .NET 委托(delegate)、事件和事件处理程序

这个问题在这里已经有了答案:Whydoestryingtounderstanddelegatesfeelliketryingtounderstandthenatureoftheuniverse?(6个答案)关闭3年前。在过去的几天里,我问了几个关于委托(delegate)的问题HERE和HERE.我承认......我真的不明白委托(delegate)。我真的真的真的很想理解和掌握它们。(我可以定义它们——类型安全的函数指针——但由于我对C类型语言的经验很少,所以它并不是很有帮助。)任何人都可以推荐一些在线资源来以一种不假设的方式解释委托(delegate)吗?这是我怀疑VB实际上妨碍了

C# 显示错误 'Delegate ' System.Func<...>' does not take 1 arguments

我打电话:form=newFormFor().Set(x=>x.Name,"hi");其中Project有一个名为Name的字段,FormFor的代码是:publicclassFormForwhereTEntity:class{FormCollectionform;publicFormFor(){form=newFormCollection();}publicFormForSet(Expression>property,stringvalue){form.Add(property.PropertyName(),value);returnthis;}}但它一直告诉我Delegate'Sy

c# - 如何将委托(delegate)存储在列表中

如何将委托(delegate)(命名的、匿名的、lambda)存储在通用列表中?基本上我正在尝试构建一个委托(delegate)字典,从中我可以使用键访问存储的委托(delegate)并执行它并按需返回值。可以在C#4中执行吗?有什么想法可以实现吗?注意:异构列表更适合我可以存储任何类型的委托(delegate)的地方。 最佳答案 是否System.Collections.Generic.Dictionary还不够吗? 关于c#-如何将委托(delegate)存储在列表中,我们在Stac

c# - 公共(public)静态字符串 MyFunc() 上的 "Expected class, delegate, enum, interface or struct"错误。什么 's an alternative to "字符串”?

当我尝试使用以下静态函数时出现错误。错误:Expectedclass,delegate,enum,interface,orstruct函数(和类):namespaceMyNamespace{publicclassMyClass{//SomeotherstaticmethodsthatuseClasses,delegates,enums,interfaces,orstructspublicstaticstringMyFunc(stringmyVar){stringmyText=myVar;//DosomestuffwithmyTextandmyVarreturnmyText;}}}这导致