是否可以“注入”?也许可以通过 app.config 将 .NET dll 放入另一个 .NET 应用程序中?

发布于 2024-08-19 23:20:44 字数 280 浏览 8 评论 0原文

我用 C# 创建了一个 .NET 类库,用于初始化一些日志记录,并将其发送到外部工具。该库完全独立于任何应用程序,但为了初始化它,我需要至少对其进行一个方法调用。

有没有办法让我将某些内容放入 app.config 中,以自动加载该 dll 并调用其中的某些内容?我可以更改内容以适合任何内容,我不需要支持任何自己的类名称或方法名称或诸如此类的内容。

请注意,我需要在不对相关应用程序进行任何更改的情况下完成此操作,除了更改 app.config 文件之外。

这可能吗?如果是这样,我应该注意什么?

I have made a .NET class library in C# that initializes some logging, sent to an external tool. The library is altogether separate from any application, but in order to initialize it, I need to make at least one method call to it.

Is there a way for me to put something into app.config that will auto-load that dll, and call something in it? I can change the contents to suit whatever, I don't need to support any own class name or method name or whatnot.

Note, I need this to be done without any changes to the application in question, save for changing the app.config file.

Is this possible? If so, what should I be looking at?

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

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

发布评论

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

评论(4

很糊涂小朋友 2024-08-26 23:20:44

它可能会被认为是一种黑客攻击,但如果您放置继承 ConfigurationSection 在你的 dll 中,并将该配置节添加到你的 app.config 中,这将允许你在配置节的构造函数中执行代码,从而完成你想要做的事情。当然,它只会在应用程序启动时被调用一次,但如果我理解你的意思是正确的,那就足够了。

It would probably be considered a hack, but if you put something that inherits ConfigurationSection in your dll, and add that configuration section to your app.config, this would allow you to execute the code in the configuration section's constructor and thus do pretty much what you want to. It will of course only be invoked once, at application start-up but if I understood you correct that would be enough.

行雁书 2024-08-26 23:20:44

这让我困惑了一段时间,不知道如何做到这一点。我最初认为可以通过创建一个配置日志记录的自定义 WebProxy 并使用 defaultProxy 配置元素。然而,这与其他配置建议存在相同的问题,因为代码仅在需要时运行(在本例中是使用 HTTP 请求时) - 因此需要对原始应用程序进行更改。

我已经实现了它尽管通过扭转方法。您可以编写一个应用程序的存根来配置日志记录,然后启动原始应用程序,而不是尝试让原始应用程序配置日志记录。

举个例子:

我有一个名为 Forms.exe 的 WinForms 应用程序,其入口点定义为:

[STAThread]
internal static void Main()
{
    Application.Run(new MainForm());
}

在我的存根应用程序(我将其作为控制台应用程序)中,我配置日志记录,然后加载并运行 Forms.exe

internal static void Main()
{
    ConfigureLogging()
    Assembly app = Assembly.LoadFrom(@".\Forms.exe");
    app.EntryPoint.Invoke(null, null);
}

这使用反射将其他应用程序加载到配置日志记录的应用程序中。

注意事项

  • 其他应用程序必须是 .Net 应用程序才能以这种方式加载它,
  • 您可能需要使用 Reflector 来检查其他应用程序以计算出传递到入口点的正确参数(即,如果它需要 string[] args,您可能需要传入一个空的 string[] 而不是 null 作为参数)
  • 原始应用程序的控制台窗口将在其他应用程序运行时挂起(这可能不是问题,但如果是问题,您可以尝试使用 FreeConsole)

This had me puzzled for a while on how to do it. I originally thought it could be achieved by creating a custom WebProxy that would configure the logging, and loading it into the main application using the defaultProxy configuration element. This however suffer from the same problem as the other configuration suggestions in that the code is only run when required (in this case when a HTTP request is used) - thus requiring a change to the original application.

I have achieved it though by reversing the approach. Instead of trying to get the original application to configure the logging, you could write a stub of an application that configures the logging and then launches the original application.

As an example:

I have a WinForms application called Forms.exe whose entry point is defined as:

[STAThread]
internal static void Main()
{
    Application.Run(new MainForm());
}

In my stub application (which I have as a console application), I configure the logging and then load and run Forms.exe:

internal static void Main()
{
    ConfigureLogging()
    Assembly app = Assembly.LoadFrom(@".\Forms.exe");
    app.EntryPoint.Invoke(null, null);
}

This uses reflection to load the other application into the one that configures the logging.

Caveats:

  • the other application has to be a .Net application in order to load it this way
  • you might need to use Reflector to inspect the other application to work out the correct arguments to pass to the entry point (ie. if it takes string[] args, you might need to pass in an empty string[] instead of a null as the arguments)
  • the original application 's console window will hang around while the other application runs (this is probably not a problem, but if it is you could try hiding it using FreeConsole)
半枫 2024-08-26 23:20:44

Snoop 使用一些 C++ 巫毒来做到这一点。幸运的是,源代码可用 - 查看名为“ManagedInjector”的项目

Snoop does this using some C++ voodoo. Fortunately the source is available - look in the project named "ManagedInjector"

a√萤火虫的光℡ 2024-08-26 23:20:44

是的,可以使用反射来加载程序集的内容

Yes, you can use reflection to load the content of assembly

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