草庐IT

delegation_target

全部标签

c# - 使用 Moq 模拟 Delegate.Invoke() 在 LINQ 中抛出 InvalidCast 异常

假设我有IService界面:publicinterfaceIService{stringName{get;set;}}还有一个委托(delegate)Func返回此接口(interface)。在我的单元测试中,我想模拟委托(delegate)的Invoke()像这样使用Moq的方法:[TestMethod]publicvoidUnitTest(){varmockService=newMock();varmockDelegate=newMock>();mockDelegate.Setup(x=>x.Invoke()).Returns(mockService.Object);//Ther

c# - : "exception was thrown by the target of invocation" C#如何解决

C#每次我运行我的程序时,我都会得到这个异常:但是当我在Debug模式下运行时,没有异常,程序运行正常,我该怎么办?注意:我没有在项目的任何地方使用invoke()编辑:好的,这是在详细信息中找到的代码:如果有人知道如何使用protoBuff,并且知道这个问题....**************ExceptionText**************System.Reflection.TargetInvocationException:Exceptionhasbeenthrownbythetargetofaninvocation.--->ProtoBuf.ProtoException:I

c# - C# 中的 IObserver 和 IObservable 用于观察者与委托(delegate)、事件

我要做的就是实现观察者模式。所以,我想到了这个解决方案:我们有一个PoliceHeadQuarters,其主要工作是向所有订阅它的人发送通知。假设DSP、Inspector和SubInspector类订阅了PoliceHeadQuarters。使用我写的事件和委托(delegate)publicclassHeadQuarters{publicdelegatevoidNewDelegate(objectsender,EventArgse);publiceventEventHandlerNewEvent;publicvoidRaiseANotification(){varhandler=t

c# - 停止非托管委托(delegate)的垃圾收集

我最近一直在尝试使用R.NET让R与.NET和C#对话。到目前为止一切顺利,但我遇到了一个我似乎无法解决的障碍。我对简单的基本命令没有任何问题。我制作了一个简单的计算器,以及将数据导入数据网格的东西。但是现在我不断收到以下错误:Acallbackwasmadeonagarbagecollecteddelegateoftype'R.NET!RDotNet.Internals.blah3::Invoke'.Thismaycauseapplicationcrashes,corruptionanddataloss.Whenpassingdelegatestounmanagedcode,they

c# - 为什么匿名委托(delegate)/lambda 不推断 out/ref 参数的类型?

StackOverflow上的几个C#问题询问如何使用out或ref参数制作匿名委托(delegate)/lambda。参见,例如:CallingamethodwithreforoutparametersfromananonymousmethodWritealambdaoranonymousfunctionthatacceptsanoutparameter为此,您只需指定参数的类型,如:publicvoiddelegateD(outTp);//...Da=(outTt)=>{...};//Lambdasyntax.Db=delegate(outTt){...};//Anonymousd

c# - 委托(delegate)返回类型与 lambda 函数不同

考虑这个MCVE:usingSystem;publicinterfaceIThing{}publicclassFoo:IThing{publicstaticFooCreate()=>newFoo();}publicclassBar:IThing{publicstaticBarCreate()=>newBar();}publicdelegateIThingThingCreator();classProgram{staticvoidTest(ThingCreatorcreator){Console.WriteLine(creator.Method.ReturnType);}staticvo

c# - 提供 .NET 方法作为委托(delegate)回调

在PowerShell中将.NET方法作为委托(delegate)回调传递给.NET对象的语法是什么。例如:C#:publicclassClass1{publicstaticvoidMyMethod(Actionobj){obj("Hey!");}}publicclassClass2{publicstaticvoidCallback(objectobj){Console.Writeline(obj.ToString());}}PowerShell:[Class1]::MyMethod([Class2]::Callback)这行不通。 最佳答案

c# - 具有四个以上参数(方法参数)的 Action 委托(delegate)

我写了一个辅助类,它使用Action-delegate作为方法参数。像这样:publicvoidSomeMethod(ActionmethodToExecute,Targument);根据MSDN,您可以声明最大值。Action委托(delegate)的4个参数:Action.现在我想调用一个需要5个的方法!争论。我怎么能这样做?最好的解决方案是我可以使用动态数量的方法参数。谢谢 最佳答案 声明你需要的Action委托(delegate),这没什么神奇的:publicdelegatevoidAction(T1p1,T2p2,T3p3

具有不同参数的两个方法的 C# 委托(delegate)

我正在使用以下方法:publicvoidM1(Int32a){//acquireMyMutexDoSomething(a);//releaseMyMutex}和publicvoidM2(Strings,Stringt){//acquireMyMutexDoSomethingElse(s,t);//releaseMyMutex}根据我目前的发现,似乎不可能对具有不同签名的两个方法使用一个委托(delegate)。有没有其他的替代方法来写这样的东西:publicvoidUsingMutex(...){//acquireMyMutex...//releaseMyMutex}UsingMute

c# - 如何在 C# 中基于现有委托(delegate)类型创建新的委托(delegate)类型?

有什么方法可以在现有委托(delegate)类型的基础上创建新的委托(delegate)类型?就我而言,我想创建一个委托(delegate)MyMouseEventDelegate这将具有与EventHandler相同的功能.我为什么要这个?当然是利用编译时类型检查!这样,我可以有两个不同的委托(delegate):MyRightClickHandler和MyLeftClickHandler,永远不要混淆这两个-即使它们在功能上都与EventHandler相同.有语法来做这种事吗?哦,代码如下:usingMyRightClickHandler=EventHandler还不够好。它不进行