Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.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#_Plugins - Fatal编程技术网

C# 哪一种方法更适合为插件提供实例?

C# 哪一种方法更适合为插件提供实例?,c#,plugins,C#,Plugins,我正在编写一个支持插件的应用程序。插件创建者负责为其创建的插件提供一些实例。提供这些实例有两种选择 选项1:Plugin writer编写插件时如下所示: public interface IGiveMe1 { INeed1 Need1 { set; } } public interface IGiveMe2 { INeed2 Need2 { set; } } public class MyPlugin : IGiveMe1, IGiveMe2 { // This has to be

我正在编写一个支持插件的应用程序。插件创建者负责为其创建的插件提供一些实例。提供这些实例有两种选择

选项1:Plugin writer编写插件时如下所示:

public interface IGiveMe1 { INeed1 Need1 { set; } }

public interface IGiveMe2 { INeed2 Need2 { set; } }

public class MyPlugin : IGiveMe1, IGiveMe2
{
    // This has to be parameterless
    MyPlugin() { ... }

    #region IGiveMe1 Members

    public INeed1 Need1 { set { ... } }

    #endregion

    #region IGiveMe2 Members

    public INeed2 Need2 { set { ... } }

    #endregion
}
public class MyPlugin
{
    MyPlugin(INeed1 need1, INeed2 need2)
    {
        ...
    }
}
插件创建者使用必需的无参数构造函数创建实例后,会查找插件类实现的接口。若插件实现了IGiveMe1,插件创建者将调用ined1的setter。同样适用于IGiveMe2.INeed2

选项2:Plugin writer编写的插件如下:

public interface IGiveMe1 { INeed1 Need1 { set; } }

public interface IGiveMe2 { INeed2 Need2 { set; } }

public class MyPlugin : IGiveMe1, IGiveMe2
{
    // This has to be parameterless
    MyPlugin() { ... }

    #region IGiveMe1 Members

    public INeed1 Need1 { set { ... } }

    #endregion

    #region IGiveMe2 Members

    public INeed2 Need2 { set { ... } }

    #endregion
}
public class MyPlugin
{
    MyPlugin(INeed1 need1, INeed2 need2)
    {
        ...
    }
}
在这个方法中,插件创建者搜索采用INeed1和INeed2类型参数的构造函数。它找到最佳匹配(具有最多匹配参数的匹配)并通过传递实例来调用它


如果要为这个应用程序编写插件,您更喜欢哪种方法?

我当然更喜欢第二种方法,因为它更简单、更清晰。插件的单元测试也将更加清晰

或者,如果您不想将所有INeedX传递给所有插件,请匹配构造函数参数的类型(显然使用反射),并只传递构造函数中接受的参数