Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建插件应用程序的方法_C#_.net_Winforms - Fatal编程技术网

C# 创建插件应用程序的方法

C# 创建插件应用程序的方法,c#,.net,winforms,C#,.net,Winforms,我有一个.NET WinForm应用程序。我想当我的应用程序运行时,它会检测到一个DLL。如果此DLL存在,我的应用程序将像内置DLL一样加载和使用它 请帮我举几个例子。谢谢。请阅读System.ComponentModel.Composition命名空间中的MEF-Microsoft扩展性框架。NET 4.0核心功能的一部分。如果它只是一个DLL,请添加对它的引用,并使用它捕获任何异常。 如果可能有许多(可能是第三方)DLL,请使用Assembly.Load*方法之一,然后可以从Assembl

我有一个.NET WinForm应用程序。我想当我的应用程序运行时,它会检测到一个DLL。如果此DLL存在,我的应用程序将像内置DLL一样加载和使用它


请帮我举几个例子。谢谢。

请阅读System.ComponentModel.Composition命名空间中的MEF-Microsoft扩展性框架。NET 4.0核心功能的一部分。

如果它只是一个DLL,请添加对它的引用,并使用它捕获任何异常。 如果可能有许多(可能是第三方)DLL,请使用
Assembly.Load*
方法之一,然后可以从Assembly对象枚举类


请参阅我的项目中的一些示例,特别是和。

您还可以使用System.Addin命名空间使用Microsoft加载项框架(MAF)


阅读有关

的更多信息如果您正在寻找使用反射的简单方法,可以采取从配置文件加载类型信息的方法:

public interface IMyPlugin
{
    void DoSomethingPlugInIsh();
}

class Program
{
    static void Main(string[] args)
    {
        IMyPlugin plugin1 = CreateFromConfig<IMyPlugin>("PluginType");
        plugin1.DoSomethingPlugInIsh();

        // etc...
    }

    static T CreateFromConfig<T>(string typeSettingName)
        where T : class
    {
        string typeName = ConfigurationManager.AppSettings[typeSettingName];
        if (string.IsNullOrEmpty(typeName))
            return null;

        var type = Type.GetType(typeName);
        return (T)Activator.CreateInstance(type);
    }

}
希望有帮助


约翰

谢谢。我不知道。我在考虑反射。我使用.NET2.0。这种情况下的任何解决方案。谢谢,是的。升级到当前版本。你落后了两代。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appSettings>
    <add key="PluginType" value="TestPlugin.MyClass, TestPlugin, Version=1.0.0.0, Culture=neutral" />
  </appSettings>
</configuration>
public class MyClass : IMyPlugin
{
    public void DoSomethingPlugInIsh()
    {
        Console.WriteLine("Hello there");
    }
}