在运行时动态加载一个程序集及其类。

huangapple go评论46阅读模式
英文:

Dynamically load an assembly and its classes at runtime

问题

I built a fairly complex system of libraries, I would like another programmer to load one of my assemblies read its properties, call its functions and listen to its events.
At the moment the end programmer would have to reference Library1 and put together with it the rest of the libraries that Library1 depends on.

To get rid of this issue I would like to make an installer that copies all the libraries to a location and the end programmer will reference just a MainLibrary that dynamically loads the rest of the libraries and exposes what needs to be used.


Type? library1Type = library1Assembly.GetType("Library1.MainClass");

dynamic library1Instance = Activator.CreateInstance(library1Type); ```

Library1 has some public classes I would like the programmer to access to though, like `User` for example. MainLibrary exposes two properties `List<User> Users` and `User SelectedUser` that Library1 already has, so I would need to create a `MainLibrary.User` class that is a complete copy of the `Library1.User` class and the same needs to happen for several other classes, custom EventArgs and so on.

I don't really like this duplication, is there any other way to achieve the same result?

<details>
<summary>英文:</summary>

I built a fairly complex system of libraries, I would like another programmer to load one of my assemblies read its properties, call its functions and listen to its events.
At the moment the end programmer would have to reference Library1 and put together with it the rest of the libraries that Library1 depends on.

To get rid of this issue I would like to make an installer that copies all the libraries to a location and the end programmer will reference just a MainLibrary that dynamically loads the rest of the libraries and exposes what needs to be used. 


Assembly library1Assembly = Assembly.LoadFrom("/path/to/where/the/libraries/where/installed/library1.dll");

Type? library1Type = library1Assembly.GetType("Library1.MainClass");

dynamic library1Instance = Activator.CreateInstance(library1Type);


Library1 has some public classes I would like the programmer to access to though, like `User` for example. MainLibrary exposes two properties `List&lt;User&gt; Users` and `User SelectedUser` that Library1 already has, so I would need to create a `MainLibrary.User` class that is a complete copy of the `Library1.User` class and the same needs to happen for several other classes, custom EventArgs and so on. 

I don&#39;t really like this duplication, is there any other way to achieve the same result?

</details>


# 答案1
**得分**: 1

我熟悉的方法是创建第三方库,例如"Interfaces",其中包含通用的类定义、接口和其他类型。这应该在两个项目之间共享。

然后,您可以使用反射来解析实现某个根接口的类型,并创建该类型的对象。从那一点上,您可以使用根对象来创建任何其他类型,或执行其他操作。

[创建一个带插件的 .NET Core 应用程序][1]文章似乎演示了这种方法。

如果可能的话,可以对这个接口进行版本控制,以允许主应用程序和库之间的一些版本不匹配。但您需要添加显式检查以避免使用任何未实现的方法/属性/类型。

[1]: https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support

<details>
<summary>英文:</summary>

The approach I&#39;m familiar with is to create a third library, say &quot;Interfaces&quot;, that contain common class definitions, interfaces, and other types. This should be shared between both projects. 

You can then use reflection to resolve the type that implements some root interface, and create an object of that type. From that point you can use the root object to create any other types, or do other things.

The [Create a .NET Core application with plugins][1] article seem to demonstrate this approach. 

If it possible to version this interface, to allow some version mismatch between the main application and the library. But you need to add explicit checks to avoid using any unimplemented method/property/type.


  [1]: https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support

</details>



huangapple
  • 本文由 发表于 2023年5月22日 16:02:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定