如何在.NET 中包装程序集?

发布于 2024-11-27 22:24:09 字数 789 浏览 1 评论 0原文

在普通的本机库中,我可以通过简单地在导出中构建具有相同函数签名的 dll 来包装另一个 dll 并监视调用,有时甚至可以调用真正的 dll。

在托管 C# 中,我有一个名为“A”的 dll,它是另一个应用程序的插件,该应用程序具有派生自另一个我称为 dll“B”的 dll 中的类的类。我想为“B”制作一个包装DLL,这样“A”就可以与“B”的包装版本一起运行,甚至可能根本不使用“B”的真实版本。

B 也有静态方法和其他类,我希望能够重新定义这个准包装器中的签名/声明,并让“A”程序集使用它。

插件 dll A:

using baseDllB;
public class foopluginA : pluginclassB
{
   public void methodbaz() { base.doStuff(); pluginclassB.doStaticStuff(); }
}

基础 dll B:

namespace baseDllB
{
   public class pluginclassB
   {
       public void doStuff()
       {
        //Do stuff
       }
       public static void doStaticStuff() { /*Do more stuff*/ }
   }
}

插件 dll 显然引用了 B,所以我想要做的是重新创建 B,在那里我能够执行日志记录等。

有哪些方法可以做到这一点?

In ordinary native libraries, I can wrap another dll and monitor calls by simply building a dll with the same exact function signatures in the export, and sometimes can even call onto the real dll.

In managed C#, I have a dll called "A" that is a plugin for another application that has a class that derives from a class in another dll I called dll "B". I want to make a wrapper dll for "B", so "A" can function with a wrapped version of "B", and might not even use the real version of "B" at all.

B also has static methods and other classes, I want to be able to redefine the signatures/declarations in this quasi-wrapper and have the "A" assembly use that instead.

Plugin dll A:

using baseDllB;
public class foopluginA : pluginclassB
{
   public void methodbaz() { base.doStuff(); pluginclassB.doStaticStuff(); }
}

Base dll B:

namespace baseDllB
{
   public class pluginclassB
   {
       public void doStuff()
       {
        //Do stuff
       }
       public static void doStaticStuff() { /*Do more stuff*/ }
   }
}

The plugin dlls reference B obviously, so what I want to do is recreate B where I'm able to perform logging, etc.

What ways are there to do this?

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

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

发布评论

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

评论(2

☆獨立☆ 2024-12-04 22:24:09

您当然可以编写一个程序集,其中有一个类将其所有方法调用和内部状态转发给其他类。但是,您必须克服一些困难。

您必须用新的包装器程序集替换对原始程序集的引用。

您必须将包装器中的类更改反映到被包装的类中。这可能很重要,特别是当内部状态包含私有成员时。如果是静态类,那就容易多了。

如果您希望包装程序集能够加载多种包装程序集,例如选择要转发到哪个类,则您需要编写一个接口并使包装程序集从该接口派生,或者您的代码在你的包装器组件中会变得非常复杂。

如果您希望包装器程序集完全动态,这意味着它在运行时加载其包装的目标,并且仅加载您想要的目标,则需要大量使用反射来从包装的类中获取方法和其他项目。

You can certainly write an assembly which had a class that forwarded all of its method calls and internal state to other classes. However, there are a few difficulties you'd have to overcome.

You would have to replace references to the original assembly with your new wrapper assembly.

You would have to reflect class changes in the wrapper to the wrapped class. This can be non-trivial, especially if the internal state contains private members. If it's a static class, that's a lot easier.

If you wanted your wrapper assembly to be able to load more than one kind of wrapped assembly, for example to choose which class to forward to, you would either need to write an interface and make the wrapped assemblies derive from that interface, or your code in your wrapper assembly would become very complex.

If you wanted your wrapper assembly to be totally dynamic, meaning it loaded its wrapped target at runtime and only the one you wanted, you'd need to make heavy use of reflection to get methods and other items from the wrapped class.

真心难拥有 2024-12-04 22:24:09

一种可能是RealProxy。您可以使用RealProxy class 来提供其他类的代理实例,客户端代码不会知道其中的区别。 这是一篇博客文章,其中包含其用法示例

One possibility is RealProxy. You can use the RealProxy class to provide proxy instances of other classes, and the client code won't know the difference. Here is a blog post with an example of its usage.

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