C# 方法调用委托

发布于 2024-07-12 04:20:26 字数 648 浏览 5 评论 0 原文

假设我有一个 DLL 程序集,其中包含一个带有两个方法的自动生成的类:

public class AutoGeneratedClass
{
    public int DoSomething(bool forReal) { ??? }
    public void DoSomethingElse(string whatever) { ??? }
}

实际上,这些方法可以是任何内容。 以上只是一个示例。

我需要生成什么样的代码来代替??? 上面让它自动打包调用的参数并将它们发送到另一个类上的单个泛型方法,然后该方法将实际处理调用并返回结果?

public class GenericClass
{
    public ??? GenericHandler(???) { ??? }
}

我想我正在寻找与 LINQ 中的 Expression> 类似的东西,它编译为可以发送到任何地方并在那里分解的数据结构。

不过,我希望保持原始方法原型与正常的本地实现方法相同,以使 AutoGenerateClass 的调用者不那么明智。

如果无法实现这一点,我有什么选择?

谢谢你!

Say I have a DLL assembly, containing an auto-generated class with two methods:

public class AutoGeneratedClass
{
    public int DoSomething(bool forReal) { ??? }
    public void DoSomethingElse(string whatever) { ??? }
}

The methods could be anything, really. The above is just an illustration.

What kind of code would I need to generate in place of ??? above to have it automatically pack up parameters of the call and send them to a single generic method on another class, which would then actually handle the call and return the result?

public class GenericClass
{
    public ??? GenericHandler(???) { ??? }
}

I guess I'm looking for something that works similar to Expression<Func<...>> in LINQ, which compiles to a data structure that can be sent anywhere and taken apart there.

I would like to keep the original method prototypes the same as if they were normal, locally implemented methods, though, to keep the caller of the AutoGeneratedClass non the wiser.

If this cannot be achieved, what are my options?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情旳空白 2024-07-19 04:20:27

我将在这里尝试一下,因为我不确定你的所有限制是什么。 这听起来像是自动生成类的部分方法/部分类的好例子。 (请注意,我还没有通过编译器运行得这么好,所以它可能需要一些清理。)

public partial class AutoGenerated
{
    public partial int ProcessDoSomething(bool forReal);
    public int DoSomething(bool forReal)
    {
        //some generated code against error conditions??
        return ProcessDoSomething(forReal)
    }

    .....
}

public partial class AutoGenerated
{
    public partial int ProcessDoSomething(bool forReal)
    {
        //call the generic handler...
    }
}

我将远离通用处理程序的类似 LINQ 的表达式部分,因为我仍在研究表达式。 然而,需要考虑的是,如果您采用类似 LINQ 的表达式路径,您的 GenericClass 维护起来有多困难/容易?

我的经验是,相当多的开发人员对委托和兰巴表达式感到不舒服。 当查看使用表达式的代码时,他们(字面意思)感到畏缩。

I'm going to take a stab in the dark here since I'm not sure what all of your limitations are. This sounds like a good case for partial methods/partial class for your AutoGenerated class. (Note I have not run this good through a compiler so it probably needs some cleanup.)

public partial class AutoGenerated
{
    public partial int ProcessDoSomething(bool forReal);
    public int DoSomething(bool forReal)
    {
        //some generated code against error conditions??
        return ProcessDoSomething(forReal)
    }

    .....
}

public partial class AutoGenerated
{
    public partial int ProcessDoSomething(bool forReal)
    {
        //call the generic handler...
    }
}

I'm going to stay away from the LINQ-like expression part of the generic handler since I'm still working on grokking Expressions. However something to consider is how hard/easy will your GenericClass be to maintain if you go down the LINQ-like expression path?

My experience has been that quite a few developers aren't comfortable with delegates and lamba expressions. They cringe (literally) when looking at code that uses Expressions.

眼睛会笑 2024-07-19 04:20:26

一定要上课吗? 使用接口的生活会更容易 - 然后你可以使用 System.Runtime.Remoting.Proxies.RealProxy 或者可能根据需要创建一个新类型。 我相信 Rhino.Mocks 会后者(使用库来完成繁重的工作) - 如果您可以使用接口,那么了解 Rhino.Mocks 的实现方式可能是一个很好的起点。

Do you have to have a class? Life would be a lot easier using interfaces - you can then use System.Runtime.Remoting.Proxies.RealProxy or possibly create a new type on demand. I believe Rhino.Mocks does the latter (using a library to do the heavy lifting) - and if you can use an interface, seeing how Rhino.Mocks is implemented may well be a good starting point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文