473,513 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# 2.0 Generic Classes

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.SoapHttpClientProtoc ol
{
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?
Nov 15 '05 #1
1 3972
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/M...aspx?id=157521

Andrew

"Rod Bass" <Ro*********@ML.com> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
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.SoapHttpClientProtoc ol
{
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?

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2877
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but...
17
3290
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
6
3954
by: Robin | last post by:
Are there any generic classes available that can be used in VB.Net to perform common tasks such as Database Access and Logging?
6
2765
by: Urs Eichmann | last post by:
While experimenting with the Feb CTP Edition of VB 2005, I came across "generic procedures". You can write: Public Class Foo Public Sub MySub(Of tDisp As IDisposable)(ByVal vMyParm As Integer)...
25
2996
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
3
2749
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
6
3450
by: Milsnips | last post by:
Hi there, this is what i'm trying to achieve, i have separate identical classes for SqlClient, OracleClient, Odbc and OleDb, what i want is to create a Wrapper class that calls any of these 4...
5
11107
by: Torben Laursen | last post by:
I am writing a COM in C# using visual studio 2005 and VSTO. Inside the code I use some support classes that are generic but they are not used in the inferface of the COM. However I still get a...
3
1970
by: Samuel R. Neff | last post by:
Is there any way to declare a static variable within a generic type definition and have that variable be shared across all constructed generic types? For example, how can I modify this code: ...
10
2691
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.