R
rkausch
Hello everyone,
I'm writing because I'm frustrated with the implementation of C#'s
generics, and need a workaround. I come from a Java background, and
am currently writing a portion of an application that needs
implementations in both Java and C#. I have the Java side done, and
it works fantastic, and the C# side is nearly there. The problem I'm
running into has to do with the differences in implementations of
Generics between the two languages. I would like to use a class that
is defined with Generics, only without having to declare its type
everywhere.
For example, suppose I'm using an IList, and in one location, I
declare it using a generic type of <String>. Now, in a different
class, I'd like to store that list as a field, but I don't want to
store the type associated with it. If I'm required to store the type
with the field (such as <pre>private IList<String> fieldName;</pre>,
then the class in which it is stored will be required to be generic,
and any classes that store instances of it will also need to be
generic, ad infinitum, until all my code is generified all over the
place (which I don't want!). Now, the example presented here is
easily solvable, since the IList interface is declared twice (once in
System.Collections as a non-generic version, and once in
System.Collections.Generic as a generic version).
Here's a code example of the Java equivalent, followed by the
(illegal) desired C# syntax:
<pre>
public interface ExampleInterface<Type>
{
// some member methods in here
}
public class ExampleImpl<Type> implements ExampleInterface<Type>
{
// method implementations here
}
public class SampleClass
{
private ExampleInterface mExampleInterface;
public void setExampleInterface(ExampleInterface
pExampleInterface)
{
mExampleInterface = pExampleInterface;
}
}
public class SampleBuilder
{
private SampleClass mSampleClass;
public SampleBuilder()
{
mSampleClass = new SampleClass();
mSampleClass.setExampleInterface(new ExampleImpl<String>());
}
}
// end Java example
</pre>
Desired C# implementation:
<pre>
public interface ExampleInterface<Type>
{
// some member methods in here
}
public class ExampleImpl<Type> : ExampleInterface<Type>
{
// method implementations here
}
public class SampleClass
{
private ExampleInterface exampleInterface;
public void SetExampleInterface(ExampleInterface exampleInterface)
{
this.exampleInterface = exampleInterface;
}
}
public class SampleBuilder
{
private SampleClass sampleClass;
public SampleBuilder()
{
this.sampleClass = new SampleClass();
this.sampleClass.SetExampleInterface(new
ExampleImpl<String>());
}
}
// end C# example
</pre>
So, in the C# example, if I'm forced to store the instance of
<pre>ExampleInterface</pre> with its generic type, then
<pre>SampleClass</pre> will have to be defined as a Generic class, and
stored in SampleBuilder with its generic type, which will then force
<pre>SampleClass</pre> to also be defined as a Generic class, and so
on and so forth.
Is there any way to do what I'd like, or am I forced to abandon the
use of Generics completely in C#?
Thanks in advance!
Respectfully,
Robert Kausch
I'm writing because I'm frustrated with the implementation of C#'s
generics, and need a workaround. I come from a Java background, and
am currently writing a portion of an application that needs
implementations in both Java and C#. I have the Java side done, and
it works fantastic, and the C# side is nearly there. The problem I'm
running into has to do with the differences in implementations of
Generics between the two languages. I would like to use a class that
is defined with Generics, only without having to declare its type
everywhere.
For example, suppose I'm using an IList, and in one location, I
declare it using a generic type of <String>. Now, in a different
class, I'd like to store that list as a field, but I don't want to
store the type associated with it. If I'm required to store the type
with the field (such as <pre>private IList<String> fieldName;</pre>,
then the class in which it is stored will be required to be generic,
and any classes that store instances of it will also need to be
generic, ad infinitum, until all my code is generified all over the
place (which I don't want!). Now, the example presented here is
easily solvable, since the IList interface is declared twice (once in
System.Collections as a non-generic version, and once in
System.Collections.Generic as a generic version).
Here's a code example of the Java equivalent, followed by the
(illegal) desired C# syntax:
<pre>
public interface ExampleInterface<Type>
{
// some member methods in here
}
public class ExampleImpl<Type> implements ExampleInterface<Type>
{
// method implementations here
}
public class SampleClass
{
private ExampleInterface mExampleInterface;
public void setExampleInterface(ExampleInterface
pExampleInterface)
{
mExampleInterface = pExampleInterface;
}
}
public class SampleBuilder
{
private SampleClass mSampleClass;
public SampleBuilder()
{
mSampleClass = new SampleClass();
mSampleClass.setExampleInterface(new ExampleImpl<String>());
}
}
// end Java example
</pre>
Desired C# implementation:
<pre>
public interface ExampleInterface<Type>
{
// some member methods in here
}
public class ExampleImpl<Type> : ExampleInterface<Type>
{
// method implementations here
}
public class SampleClass
{
private ExampleInterface exampleInterface;
public void SetExampleInterface(ExampleInterface exampleInterface)
{
this.exampleInterface = exampleInterface;
}
}
public class SampleBuilder
{
private SampleClass sampleClass;
public SampleBuilder()
{
this.sampleClass = new SampleClass();
this.sampleClass.SetExampleInterface(new
ExampleImpl<String>());
}
}
// end C# example
</pre>
So, in the C# example, if I'm forced to store the instance of
<pre>ExampleInterface</pre> with its generic type, then
<pre>SampleClass</pre> will have to be defined as a Generic class, and
stored in SampleBuilder with its generic type, which will then force
<pre>SampleClass</pre> to also be defined as a Generic class, and so
on and so forth.
Is there any way to do what I'd like, or am I forced to abandon the
use of Generics completely in C#?
Thanks in advance!
Respectfully,
Robert Kausch