能否在X++代码中使用C# .dll文件,而不将其添加到引用中?

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

Is it possible to use C# .dll file in X++ code without adding it into reference?

问题

I have the following problem: we developed a C# class which we are planning to use in X++ classes. So, we created a .dll file and added it through reference in X++ project.

However, if we upload the code to the repository and clone it into other machine and try to build the model we will get the bunch of errors where X++ classes which use the C# class can't find it. So we have to add this .dll file again.

I wanted to ask if it is possible for the X++ class to find this .dll file dynamically without adding references each time? It is important for us because it can cause some problems in the future.

I would be really glad if you help to solve this issue or provide some advice.

英文:

I have the following problem: we developed a C# class which we are planning to use in X++ classes. So, we created a .dll file and added it through reference in X++ project.

However, if we upload the code to the repository and clone it into other machine and try to build the model we will get the bunch of errors where X++ classes which use the C# class can't find it. So we have to add this .dll file again.

I wanted to ask if it is possible for the X++ class to find this .dll file dynamicaly without adding references each time? It is important for us because it can cause some problems in future.

I would be really glad if you help to solve this issue or provide some advice.

答案1

得分: 1

你可以通过将其路径存储在配置文件或数据库中,然后让应用程序访问来动态定位和加载 DLL。这样,你的应用程序可以在运行时读取 DLL 的路径并动态加载它。

这个位置可以是网络共享、与所有环境一致的磁盘位置,甚至是云存储位置。
以下是一个通用示例:

System.Reflection.Assembly assembly;
System.Type type;
System.Object obj;
System.Reflection.MethodInfo method;

// 加载程序集
assembly = System.Reflection.Assembly::LoadFrom(dllPath);

// 获取类型
type = assembly.GetType("YourNamespace.YourClass");

// 创建类型的实例
obj = System.Activator::CreateInstance(type);

// 获取方法
method = type.GetMethod("YourMethod");

// 调用方法
var result = method.Invoke(obj, null);

在这个示例中,ConfigurationManager.AppSettings["DllPath"] 从配置文件中读取 DLL 的路径。键 "DllPath" 应该在你的配置文件中定义,并包含指向 DLL 的路径。

请注意,你需要拥有访问存储 DLL 的位置的适当访问权限。而且,DLL 应该与你的应用程序的平台(x86、x64)兼容。

英文:

You can dynamically locate and load the DLL by storing its path in a configuration file or a database that your application can access. This way, your application can read the path to the DLL at runtime and load it dynamically.

The location could be a network share, a location on disk that is consistent across all environments, or even a cloud storage location.
Here is a general example:

System.Reflection.Assembly assembly;
System.Type type;
System.Object obj;
System.Reflection.MethodInfo method;
;

// Load assembly
assembly = System.Reflection.Assembly::LoadFrom(dllPath);

// Get type
type = assembly.GetType("YourNamespace.YourClass");

// Create an instance of the type
obj = System.Activator::CreateInstance(type);

// Get method
method = type.GetMethod("YourMethod");

// Invoke method
var result = method.Invoke(obj, null);

In this example, ConfigurationManager.AppSettings["DllPath"] reads the DLL path from your configuration file. The key "DllPath" should be defined in your configuration file with the path to your DLL.

Please note, you need to have proper access rights to the location where the DLL is stored. And the DLL should be compatible with the platform (x86, x64) of your application.

huangapple
  • 本文由 发表于 2023年5月14日 15:09:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246269.html
匿名

发表评论

匿名网友

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

确定