I'm building an application with WPF and C#, basically I want to allow anyone to create a dll and put it into a folder (similar to plugins). The application will take all the dlls on the folder, load them and use their methods (defined on an interface).
我正在使用WPF和C#构建应用程序,基本上我想允许任何人创建一个dll并将其放入一个文件夹(类似于插件)。应用程序将获取文件夹上的所有dll,加载它们并使用它们的方法(在接口上定义)。
Any ideas how to reference the dlls on runtime? Any better idea how to implement this?
有关如何在运行时引用dll的任何想法?任何更好的想法如何实现这一点?
5 个解决方案
#1
9
I've implemented something like you are asking for that searches through dlls in a given directory and finds classes that implement a particular interface. Below is the class I used to do this:
我实现了类似于你要求通过给定目录中的dll搜索并找到实现特定接口的类的东西。以下是我以前用过的课程:
public class PlugInFactory<T>
{
public T CreatePlugin(string path)
{
foreach (string file in Directory.GetFiles(path, "*.dll"))
{
foreach (Type assemblyType in Assembly.LoadFrom(file).GetTypes())
{
Type interfaceType = assemblyType.GetInterface(typeof(T).FullName);
if (interfaceType != null)
{
return (T)Activator.CreateInstance(assemblyType);
}
}
}
return default(T);
}
}
All you have to do is initialize this class with something like this:
您所要做的就是使用以下内容初始化此类:
PlugInFactory<InterfaceToSearchFor> loader = new PlugInFactory<InterfaceToSearchFor>();
InterfaceToSearchFor instanceOfInterface = loader.CreatePlugin(AppDomain.CurrentDomain.BaseDirectory);
If this answer or any of the other answers help you in solving your problem please mark it as the answer by clicking the checkmark. Also if you feel like it's a good solution upvote it to show your appreciation. Just thought I'd mention it since it doesn't look like you accepted answers on any of your other questions.
如果此答案或任何其他答案可帮助您解决问题,请单击复选标记将其标记为答案。此外,如果您觉得这是一个很好的解决方案,请表达您的赞赏。只是想到我会提到它,因为看起来你没有接受任何其他问题的答案。
#2
1
Have a look at MEF. it should provide exactly what you are looking for.
看看MEF。它应该提供你正在寻找的东西。
Using reflection is also an option, but MEF will be a better choice if you ask me.
使用反射也是一种选择,但如果你问我,MEF将是更好的选择。
#3
1
I think you can start of with something like
我想你可以从类似的东西开始
Assembly assembly = Assembly.LoadFrom("Something.dll");
Type type = assembly.GetType("SomeType");
object instanceOfSomeType = Activator.CreateInstance(type);
Then you can use it
然后你可以使用它
#4
0
string relative = "ClassLibrary1.dll";
string absolute = Path.GetFullPath(relative);
Assembly assembly = Assembly.LoadFile(absolute);
System.Type assemblytype = assembly.GetType("ClassLibrary1.Class1");
object []argtoppass={1,2};
var a =Activator.CreateInstance(assemblytype, argtoppass);
System.Type type = a.GetType();
if (type != null)
{
string methodName = "add";
MethodInfo methodInfo = type.GetMethod(methodName);
object result = methodInfo.Invoke(a, null);
int a1 = (int )result;
}
#5
0
I am working on something similar, where a customer may have different versions of a third-party DLL installed, though the name and location are the same. If we don't reference the right version, we get failures. I am using reflection to identify the version, but I need to either modify the setup to use a different DAL class depending on the version, or make the DAL version independent through an interface.
我正在开发类似的东西,其中客户可能安装了不同版本的第三方DLL,但名称和位置相同。如果我们没有引用正确的版本,我们就会失败。我使用反射来识别版本,但我需要修改设置以根据版本使用不同的DAL类,或者通过接口使DAL版本独立。
I am leaning toward the second. If you make a "dummy DLL in your code that implements an interface that has all the methods you want to invoke from the target assembly, and pass in not the actual type as a parameter but the interface, then you should be able to use all the methods in your design-time code and compile based on your mock-up implementations, and even do your testing, but then when you load the actual assembly at runtime get the results from the real assembly. Let me know if this works for you, and I will let you know as well.
我倾向于第二个。如果您在代码中创建一个“虚拟DLL”,它实现了一个接口,该接口具有您想要从目标程序集调用的所有方法,并且不是将实际类型作为参数传递给接口,那么您应该能够使用所有您的设计时代码中的方法和基于您的模型实现进行编译,甚至进行测试,但是当您在运行时加载实际的程序集时,从实际程序集中获取结果。让我知道这是否适合您,我也会告诉你的。
Joey Morgan
乔伊摩根
#1
9
I've implemented something like you are asking for that searches through dlls in a given directory and finds classes that implement a particular interface. Below is the class I used to do this:
我实现了类似于你要求通过给定目录中的dll搜索并找到实现特定接口的类的东西。以下是我以前用过的课程:
public class PlugInFactory<T>
{
public T CreatePlugin(string path)
{
foreach (string file in Directory.GetFiles(path, "*.dll"))
{
foreach (Type assemblyType in Assembly.LoadFrom(file).GetTypes())
{
Type interfaceType = assemblyType.GetInterface(typeof(T).FullName);
if (interfaceType != null)
{
return (T)Activator.CreateInstance(assemblyType);
}
}
}
return default(T);
}
}
All you have to do is initialize this class with something like this:
您所要做的就是使用以下内容初始化此类:
PlugInFactory<InterfaceToSearchFor> loader = new PlugInFactory<InterfaceToSearchFor>();
InterfaceToSearchFor instanceOfInterface = loader.CreatePlugin(AppDomain.CurrentDomain.BaseDirectory);
If this answer or any of the other answers help you in solving your problem please mark it as the answer by clicking the checkmark. Also if you feel like it's a good solution upvote it to show your appreciation. Just thought I'd mention it since it doesn't look like you accepted answers on any of your other questions.
如果此答案或任何其他答案可帮助您解决问题,请单击复选标记将其标记为答案。此外,如果您觉得这是一个很好的解决方案,请表达您的赞赏。只是想到我会提到它,因为看起来你没有接受任何其他问题的答案。
#2
1
Have a look at MEF. it should provide exactly what you are looking for.
看看MEF。它应该提供你正在寻找的东西。
Using reflection is also an option, but MEF will be a better choice if you ask me.
使用反射也是一种选择,但如果你问我,MEF将是更好的选择。
#3
1
I think you can start of with something like
我想你可以从类似的东西开始
Assembly assembly = Assembly.LoadFrom("Something.dll");
Type type = assembly.GetType("SomeType");
object instanceOfSomeType = Activator.CreateInstance(type);
Then you can use it
然后你可以使用它
#4
0
string relative = "ClassLibrary1.dll";
string absolute = Path.GetFullPath(relative);
Assembly assembly = Assembly.LoadFile(absolute);
System.Type assemblytype = assembly.GetType("ClassLibrary1.Class1");
object []argtoppass={1,2};
var a =Activator.CreateInstance(assemblytype, argtoppass);
System.Type type = a.GetType();
if (type != null)
{
string methodName = "add";
MethodInfo methodInfo = type.GetMethod(methodName);
object result = methodInfo.Invoke(a, null);
int a1 = (int )result;
}
#5
0
I am working on something similar, where a customer may have different versions of a third-party DLL installed, though the name and location are the same. If we don't reference the right version, we get failures. I am using reflection to identify the version, but I need to either modify the setup to use a different DAL class depending on the version, or make the DAL version independent through an interface.
我正在开发类似的东西,其中客户可能安装了不同版本的第三方DLL,但名称和位置相同。如果我们没有引用正确的版本,我们就会失败。我使用反射来识别版本,但我需要修改设置以根据版本使用不同的DAL类,或者通过接口使DAL版本独立。
I am leaning toward the second. If you make a "dummy DLL in your code that implements an interface that has all the methods you want to invoke from the target assembly, and pass in not the actual type as a parameter but the interface, then you should be able to use all the methods in your design-time code and compile based on your mock-up implementations, and even do your testing, but then when you load the actual assembly at runtime get the results from the real assembly. Let me know if this works for you, and I will let you know as well.
我倾向于第二个。如果您在代码中创建一个“虚拟DLL”,它实现了一个接口,该接口具有您想要从目标程序集调用的所有方法,并且不是将实际类型作为参数传递给接口,那么您应该能够使用所有您的设计时代码中的方法和基于您的模型实现进行编译,甚至进行测试,但是当您在运行时加载实际的程序集时,从实际程序集中获取结果。让我知道这是否适合您,我也会告诉你的。
Joey Morgan
乔伊摩根