如何实现通用接口的工厂模式?

时间:2019-08-17 19:59:35

标签: c# vb.net generics interface factory

我在VB项目中具有以下接口和类实现:

Public Interface IEpmWrapper(Of TEpmInput As {IEpmInput, Class})

    Function Run(input As TEpmInput) As IEpmResult
    Sub InitializeData()

End Interface
Public Class TF2spPMFanSec_Wrapper
    Implements IEpmWrapper(Of TF2spPMFanSec_EpmInput)

    Public Sub InitializeData() Implements IEpmWrapper(Of TF2spPMFanSec_EpmInput).InitializeData
        ...
    End Sub

    Public Function Run(input As TF2spPMFanSec_EpmInput) As IEpmResult Implements IEpmWrapper(Of TF2spPMFanSec_EpmInput).Run
        ...
    End Function

End Class

因此,现在在我的C#项目中,我需要实现一个工厂,该工厂基于枚举返回这些包装器之一,如下所示:

public interface IEpmFactory
{
        IEpmWrapper<TEpmInput> CreateEpm<TEpmInput>(EpmConfigurationType configurationType)
            where TEpmInput: class, IEpmInput;
}
public class EpmFactory : IEpmFactory
{
    public IEpmWrapper<TEpmInput> CreateEpm<TEpmInput>(EpmConfigurationType configurationType) where TEpmInput : class, IEpmInput
    {
        switch (configurationType)
        {
            case EpmConfigurationType.TwoSpoolPartialMixFanSecTurbofan:
                return new TF2spPMFanSec_Wrapper() as IEpmWrapper<TF2spPMFanSec_EpmInput>;
            default:
                throw new NotImplementedException();
        }
    }
}

上面的工厂代码段不起作用,因为无法隐式完成转换。

(。NET Core 2.2,.NET标准2.0)

我该如何设置?

0 个答案:

没有答案