C# 2.0 Generic Classes

  • Thread starter Thread starter Rod Bass
  • Start date Start date
R

Rod Bass

I was disappointed to see that you can't use a parameter type as a base
class for a generic class. I would find it extremely useful to extend a
generic type of class (as is done nicely using C++ templates). For example:

public class SoapClientProtocolWithHeaders<T>: T
where T: System.Web.Services.Protocols.SoapHttpClientProtocol
{
protected NameValueCollection m_Headers = new NameValueCollection();

public NameValueCollection Headers { get { return m_Headers; } }

protected override System.Net.WebRequest GetWebRequest(System.Uri uri)
{
System.Net.WebRequest wr = base.GetWebRequest(uri);
wr.Headers.Add(m_Headers);
return wr;
}
}


I currently use the CodeDOM to gen this code in memory and I was looking
forward to converting it to a generic class. I would guess that you must
have contemplated this and there a good reason that this cannot be done?
 
I had the same question but if you look at the tradeoffs the
language designers had to make it makes sense that deriving
from a naked type parameter is not allowed. Since the generic
code is shared between all reference types the compiler knows
the vtable layout and things are easy. With unknown base
classes there would need to be some mechanism to "find" the
right function to call on T.

Anders replied to my question in the C# forum:
http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=157521

Andrew
 
Back
Top