Sunday, August 7, 2011

C# - generic type properties with arguments on initialization

Abstract Base Class

public abstract class CADWebServiceBase<T> where T : class
{
    protected dynamic CustomHeader;
    abstract protected String WebServiceName { get; } 

    protected T _service;
    protected T Service
    {
        get
        {
            if (this._service == null)
                this._service = (T)Activator.CreateInstance(typeof(T), this.WebServiceName);
            return this._service;
        }
        set
        {
            this._service = value;
        }
    }
}

Child Class

public class CADGeographyWebService : CADWebServiceBase<CADGeographyServiceSoapClient>
{
    protected override string WebServiceName
    {
        get { return CADWebServices.SOAP_Geography; }
    }

    public String HelloWorld()
    {
        String results = this.Service.HelloWord();
          
        return results;
    }

}

No comments:

Post a Comment