我可以将一个 c# 类重定向到另一个 dll 吗?

问题描述

这是我的问题。有一个带有核心库 core2.dll 的游戏引擎,其中包含一个 C 类。

它是一个带有 c# 包装器的 windows 类。

我想主要使用 core2.dll 中的所有类,除了一个类,我想使用库 core1.dll 的先前版本中的类

如果您尝试从 core2.dll 调用该类,它会显示“已弃用的错误消息”。

我认为这门课非常自给自足。 (假设它是一些数学函数)所以我的想法是在我的项目中同时包含 core2.dll 和 core1.dll,但不知何故只从 core1.dll 生成 C 类。

对如何(如果有的话)实现这一目标有任何想法吗?

解决方法

如果你需要替换 core2.dll 中的部分代码,那么你可以使用 ildasm 反编译两者,更改 core2.dll 中需要的部分,然后重新编译。

运行x64_x86 Cross Tools Command Prompt for VS 2017并执行

ildasm core1.dll /OUT=core1.il
ildasm core2.dll /OUT=core2.il


REM open il files with text editor,find classes in core1.il and replace them in core2.il 
REM note,that there might be no core2.res

ilasm core2.il /RESOURCE=core2.res /DLL

如果 dll 未签名,这将完全正常工作。更好 - 您组装成代码,因此 ilasm 将确保您的代码在运行时工作。运行时不兼容是绑定重定向的一个非常烦人的问题...

确保您没有违反任何许可和版权。

,

您需要为两个 dll 使用外部别名

这是一个例子: https://www.learningsomethingnew.com/how-to-use-two-versions-of-the-same-dll-in-the-same-project