Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:53:18+00:00 2026-06-13T16:53:18+00:00

I realize it is not possible to derive from a generic type parameter, and

  • 0

I realize it is not possible to derive from a generic type parameter, and I understand all the complications that arise if it is allowed.

  • Generics FAQ
  • Why it can’t be done

So my question is, how do I work around this? I am trying to eliminate some duplicate code and the best solution (which is not a solution because it cannot be done) I have come up with would be to do something like this:

public class Proxy<T> : T where T : CommonBaseClass

The reason I would want to do this is to override some methods in the CommonBaseClass.

Here is some actual code examples from my project:

public class ReportingServiceProxy : ReportingService2010
{
    protected override WebResponse GetWebResponse (WebRequest request)
    {
        WebResponse response = base.GetWebResponse(request);
        // Do override stuff here
        return response;
    }
}

public class ReportingExecutionProxy : ReportExecution2005
{
    protected override WebResponse GetWebResponse (WebRequest request)
    {
        WebResponse response = base.GetWebResponse(request);
        // Do override stuff here
        return response;
    }
}

In the example above the ReportingService2010 & ReportExecution2005 classes both derive from SoapHttpClientProtocol which derives from HttpWebClientProtocol. The override methods GetWebResponse and GetWebRequest both override methods in HttpWebClientProtocol and are identical. These methods are where the code duplication exists which I am trying to refactor out.

For completeness, here is some additional code where I implement the code above:

public abstract class SSRSReportBase<T> where T : new()
{        
    protected T ssrs;
    protected abstract void ServiceLogon();

    public SSRSReportBase()
    {
        ServiceLogon();
        // Do other common constructor work here.
    }
}

// Implementation #1
public class SSRSReportExecution : SSRSReportBase<ReportExecutionProxy>
{
    protected override void ServiceLogon()
    {
        ssrs.LogonUser("LoginName", null, null);
    }

    // Create additional wrapper methods for ReportExecution2005
}

// Implementation #2
public class SSRSReportingService : SSRSReportBase<ReportingServiceProxy>
{
    protected override void ServiceLogon()
    {
        ssrs.LogonUser("LoginName", null, null);
    }

    // Create additional wrapper methods for ReportingService2010
}

The only solution I have come up with is not a possible solution, so obviously it’s NOT a solution. Here is the bad code:

public class ReportProxy<T> : T where T : HttpWebClientProtocol, new()
{
    protected override WebResponse GetWebResponse (WebRequest request)
    {
        WebResponse response = base.GetWebResponse(request);
        return response;
    }
}

// Implementation #1
public class SSRSReportExecution : SSRSReportBase<ReportProxy<ReportExecution2005>>
{
}

// Implementation #2
public class SSRSReportingService : SSRSReportBase<ReportProxy<ReportingService2010>>
{
}

Question: So my question is, how can this code be refactored in such a way as to remove the duplicate code (the GetWebRequest and GetWebResponse overrides)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:53:19+00:00Added an answer on June 13, 2026 at 4:53 pm

    A solution would be to not use generics at all and to use a factory pattern. Instead of having a generic parameter with the BaseClass and the new() constraint. Type everything BaseClass and provide a factory for new(). This can be either a full-fleged factory class or a lightweight factory delegate.

    public interface IProtocolFactory
    {
        ProtocolBaseClass Create();
    }
    
    public class SomeDerivedProtocolFactory : IProtocolFactory
    {
        public ProtocolBaseClass Create()
        {
            return new SomeDerivedProtocol();
        }
    }
    

    You might even add additional initialization code in the factory.

    public class ReportProxy
    {
        private IProtocolFactory _factory;
    
        public ReportProxy(IProtocolFactory factory)
        {
            _factory = factory;
        }
    
        public void DoSomething()
        {
            ProtocolBaseClass protocol = _factory.Create();
            ...
        }
    }
    

    Using a delegate

        private Func<ProtocolBaseClass> _createProtocol;
    
        public ReportProxy(Func<ProtocolBaseClass> createProtocol)
        {
            _createProtocol= createProtocol;
        }
    
        public void DoSomething()
        {
            ProtocolBaseClass protocol = _createProtocol();
            ...
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: String comparison in Objective-C I realize that the question is not very
I realize that virtual template functions are not allowed in c++. Because of my
I realize it's a chicken and egg problem and that it's not possible to
I realize that this is not possible since Android doesnt have a JVM but
I just can't realize why my read time out is not working. All I
It's not possible to inherit from a C# struct. It's not obvious to me
I fully realize that what I am proposing does not follow the .NET guidelines,
I realize that this is probably not the smartest thing to do with regular
I realize I'm going to get flamed for not simply writing a test myself...
I realize this question has probably been asked numerous times, but I have not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.