将我自己的参数发送给事件处理程序?

Sending My Own Arguments To A Event Handler?(将我自己的参数发送给事件处理程序?)
本文介绍了将我自己的参数发送给事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在专门使用 AssemblyResolve.这是我的代码,然后是我的问题:

I'm working with AssemblyResolve specifically. Here is my code first, then my question follows:

var a = AppDomain.CurrentDomain;
a.AssemblyResolve += new ResolveEventHandler(HandleIt);

Private Assembly HandleIt(object sender, ResolveEventArgs args){
    //Does stuff, returns an assembly
}

所以我将 HandleIt 添加到我的 AssemblyResolve 事件中.如何将它添加到该事件并传递一个参数,例如:

So I add HandleIt to my AssemblyResolve event. How can I add it to that event and pass an argument with it like:

a.AssemblyResolve += new ResolveEventHandler(HandleIt(AnArgument));

这让我很失望,因为 HandleIt 需要参数,但是当它被添加到 AssemblyResolve 事件时,没有一个是明确传递的.我希望是这样的:

This is throwing me off since HandleIt takes arguments, but none are explicetly passed when it is added to the AssemblyResolve event. I would expect something like:

a.AssemblyResolve += new ResolveEventHandler(HandleIt(aSender,someArgs));

是的,我只想在将另一个参数添加到我的 AssemblyResolve 事件时向我的 HandleIt 函数发送另一个参数.

So yeah, I just want to be able to send another argument to my HandleIt function when adding it to my AssemblyResolve event.

希望这是有道理的,谢谢.

Hope that makes sense, thanks.

附录:

if(aBool){
    a.AssemblyResolve += new ResolveEventHandler(HandleA);
}
else{
    a.AssemblyResolve += new ResolveEventHandler(HandleB);
}

HandleA(object sender, ResolveEventArgs args){
    Handle(true);
}
HandleB(object sender, ResolveEventArgs args){
    Handle(false);
}
Handle(bool isA){

}

-vs-

if(aBool){
    a.AssemblyResolve += (object s, ResolveEventArgs a) => Handle(s,a,true);
}
else{
    a.AssemblyResolve += (object s, ResolveEventArgs a) => Handle(s,a,false);
}

Handle(object sender, ResolveEventArgs args, bool isA){

}

推荐答案

当事件被触发时,参数被传递给方法,如果你想绑定额外的参数,你可以使用 lambda 表达式来做到这一点

When the event is fired arguments are passed to the method if you would like to bind additional arguments you could do that with a lambdaexpression

var a = AppDomain.CurrentDomain;
a.AssemblyResolve += (object s,ResolveEventArgs a) => HandleIt(s,a,someArgument);

Private Assembly HandleIt(object sender, ResolveEventArgs args, SomeType arg){
    //Does stuff, returns an assembly
}

其中 someArgument 是您希望绑定的值.

where someArgument is the value you wish to bind.

这本质上是使用 lambdas 对函数进行部分应用.C# 不直接支持但在其他语言中很常见的东西.部分应用程序与 Currying 密切相关,当然,这种语言存在于 F# 和 Haskell 等语言中(因为这个概念的名称来自 Haskell Curry)以及各种其他功能语言.它们的结果类型不同.

This is essentially using lambdas to do a partial application of a function. Something that C# doesn't support directly but is very common in other languages. Partial application is very closely related to Currying that exists in languages such as F# and Haskell of course (since the concept gets it's name from Haskell Curry) and various other functional langauages. They differ in the result type.

它们都与闭包相关(因为上面代码中的概念被称为),并且在不支持部分应用或柯里化的语言中,您可以使用闭包来完成类似的事情.但是请注意,闭包与部分应用程序的不同之处在于可能会产生一些令人惊讶的错误.例如.

They are both related to closures (as the concept in the above code is called) and in languages that don't support partial application or currying you can use closures to accomplish something similar. However be aware that closures differ from partial application in ways that can create some surprising bugs. E.g.

int i = 1;
Func<int> f = () => i;
i = 2;

System.Console.WriteLine(f());

2 打印到控制台.因为闭包捕获了对变量的引用,而不是所述变量的值.这是 for 循环中的常见错误,当关闭 for 循环的循环变量时.

prints 2 to the console. Because closures captures a reference to a variable not the value of said variable. This is a common error in for loops, when closing over the loop variable(s) of the for loop.

这篇关于将我自己的参数发送给事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)