简体   繁体   English

泛型C# - 从静态泛型类创建实例

[英]Generics C# - creating an instance from a Static Generic Class

I have a factory class in my application but due to my lack of knowledge around generics I am not sure how to go about creating an instance of this particular type. 我的应用程序中有一个工厂类,但由于我对泛型缺乏了解,我不知道如何创建这种特殊类型的实例。

Would someone be kind enough to point me how to call this class so that it returns a service... Any help would be kindly appreciated. 有人会友好地指出我如何调用这个类,以便它返回一个服务...任何帮助将不胜感激。

public class ServiceFactory<TService> where TService : WebServicesClientProtocol, new()
{

    public static TService GetSecureService(string serviceUrl, X509Certificate certificate)
    {
        if (serviceUrl == null)
        {
            throw new ArgumentNullException(nameof(serviceUrl), "Service URL must be specified.");
        }

        if (serviceUrl == string.Empty)
        {
            throw new ArgumentException("Service URL cannot be empty.", nameof(serviceUrl));
        }

        if (certificate == null)
        {
            throw new ArgumentNullException(nameof(certificate), "Client certificate must be specified.");
        }

        return Service(serviceUrl, certificate);
    }

    private static TService Service(string serviceUrl, X509Certificate certificate)
    {
        return ConfigureService((TService)Activator.CreateInstance(typeof(TService)), serviceUrl, certificate);
    }

    public static TService ConfigureService(TService service, string serviceUrl, X509Certificate certificate)
    {
        service.PreAuthenticate = true;
        service.Credentials = CredentialCache.DefaultCredentials;
        service.Timeout = 250;
        service.Url = serviceUrl;

        return ConfigureRequestSignature(service, certificate);
    }

    private static TService ConfigureRequestSignature(TService service, X509Certificate certificate)
    {
        X509SecurityToken signatureToken = new X509SecurityToken(certificate);
        MessageSignature signature = new MessageSignature(signatureToken);

        signature.SignatureOptions = SignatureOptions.IncludeTimestamp | SignatureOptions.IncludeSoapBody | SignatureOptions.IncludeMessageId | SignatureOptions.IncludeAction;

        SoapContext requestContext = service.RequestSoapContext;
        requestContext.Security.Tokens.Add(signatureToken);
        requestContext.Security.Elements.Add(signature);

        // TTL must be less than 60 seconds, otherwise the request will be discarded.
        requestContext.Security.Timestamp.TtlInSeconds = 50;

        return service;
    }
}

在不知道可以实例化的类的名称的情况下......

var service = ServiceFactory<ConcreteImplementationService>.GetSecureService(url, cert);

Since you're using static methods, use method level generic parameters: 由于您使用的是静态方法,因此请使用方法级通用参数:

TService GetSecureService<TService>(string serviceUrl, X509Certificate certificate)
   where TService : WebServicesClientProtocol, new()
{
...
     return Service<TService>(serviceUrl, certificate);
}

You will probably find all the methods will use this pattern and pass the generic type down as I did when calling Service() above. 您可能会发现所有方法都将使用此模式并按照我上面调用Service()时的方式传递泛型类型。

MSDN Article for generic methods: https://msdn.microsoft.com/en-us/library/twcad0zb.aspx MSDN文章的通用方法: https//msdn.microsoft.com/en-us/library/twcad0zb.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM