如何在 .NET 中包装程序集? [英] How to wrap assemblies in .NET?

查看:38
本文介绍了如何在 .NET 中包装程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

在托管 C# 中,我有一个名为A"的 dll,它是另一个应用程序的插件,该应用程序的类派生自另一个我称为 dllB"的 dll 中的类.我想为B"制作一个包装 dll,因此A"可以使用B"的包装版本运行,甚至可能根本不使用B"的真实版本.

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 也有静态方法和其他类,我希望能够在这个准包装器中重新定义签名/声明,并让A"程序集使用它.

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.

插件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,我可以在其中执行日志记录等.

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?

推荐答案

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

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.

这篇关于如何在 .NET 中包装程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆