参考.NET
References .NET
我有一个项目 (A) 将开发一个记录器系统。该项目将用于其他项目。在项目 A 中,有两个引用未在编译时使用。
当我在任何项目中添加对项目 A 的引用时,不会导入引用,但我在运行时需要它们。
我见过两种解决方案:
a) 在编译时使用伪代码强制复制。这个解决方案似乎不够优雅。
b) 添加其他项目中的引用。此解决方案强制始终引用 DLL
有没有强制复制引用的解决方案?
非常感谢
考虑制作一个 NuGet package for you project. A NuGet package can ship the run time required libraries and add them to the target project (although I think you may need to use the PowerShell 脚本来做到这一点。
然后您可以在目标项目中使用 "Manage NuGet references" 添加您的 "Project A" 并让 NuGet 安装脚本添加 "runtime only" 库引用
请记住,您可以在内部托管自己的 NuGet 包,无需将它们发布到在线画廊。我们将这种方法用于内部共享项目,因为它使依赖关系管理和版本控制变得非常简单和明智,并在源代码控制方面保持每个项目的严格范围。
尝试更改项目 A 中指定引用的 Copy Local 属性
编辑作为此案例的解决方案:
If you are not using those references in compile time then you should
add the specific dll files ( add existing item ), as content in Build
Action, and with Copy Always option ( in project A ). When the project
A gets build, those files will be the build output. This way you can
access the files form other projects referencing project A
您可以使用反射在运行时加载程序集:
Assembly assembly = Assembly.LoadFile(path);
Type t = assembly.GetType("YourClassName");
object instance = Activator.CreateInstance(t);
int parameter1 = 1;
object result = t.GetMethod("The MethodName").Invoke(instance, new object[] { parameter1, "Something" });
虽然有点麻烦。
也许可以尝试解决问题的根源 - 为什么不添加对实际使用它们的项目的引用?
我有一个项目 (A) 将开发一个记录器系统。该项目将用于其他项目。在项目 A 中,有两个引用未在编译时使用。
当我在任何项目中添加对项目 A 的引用时,不会导入引用,但我在运行时需要它们。
我见过两种解决方案:
a) 在编译时使用伪代码强制复制。这个解决方案似乎不够优雅。
b) 添加其他项目中的引用。此解决方案强制始终引用 DLL
有没有强制复制引用的解决方案?
非常感谢
考虑制作一个 NuGet package for you project. A NuGet package can ship the run time required libraries and add them to the target project (although I think you may need to use the PowerShell 脚本来做到这一点。
然后您可以在目标项目中使用 "Manage NuGet references" 添加您的 "Project A" 并让 NuGet 安装脚本添加 "runtime only" 库引用
请记住,您可以在内部托管自己的 NuGet 包,无需将它们发布到在线画廊。我们将这种方法用于内部共享项目,因为它使依赖关系管理和版本控制变得非常简单和明智,并在源代码控制方面保持每个项目的严格范围。
尝试更改项目 A 中指定引用的 Copy Local 属性
编辑作为此案例的解决方案:
If you are not using those references in compile time then you should add the specific dll files ( add existing item ), as content in Build Action, and with Copy Always option ( in project A ). When the project A gets build, those files will be the build output. This way you can access the files form other projects referencing project A
您可以使用反射在运行时加载程序集:
Assembly assembly = Assembly.LoadFile(path);
Type t = assembly.GetType("YourClassName");
object instance = Activator.CreateInstance(t);
int parameter1 = 1;
object result = t.GetMethod("The MethodName").Invoke(instance, new object[] { parameter1, "Something" });
虽然有点麻烦。
也许可以尝试解决问题的根源 - 为什么不添加对实际使用它们的项目的引用?