M
Martin Robins
I do not know if what I am trying to do is impossible, however since I cannot work out how to do it I thought I would post it here in the hope of some assistance.
Consider the following code ...
using System;
public class Parameter<T> {
private T value;
public Parameter() {
}
public Parameter(T value)
: this() {
this.value = value;
}
public T Value {
get { return this.value; }
set { this.value = value; }
}
}
public class BooleanParameter : Parameter<Boolean> {
}
public class ByteParameter : Parameter<Byte> {
}
public class WordParameter : Parameter<UInt16> {
}
As you can see, this is a simple example where I am creating generic types that will all expose a 'Value' property, but each of the 'Value' properties will have a different data type.
If I want to create collections for these types, again this is fairly basic ...
using System.Collections.ObjectModel;
public class BooleanParameterCollection : Collection<BooleanParameter> {
}
public class ByteParameterCollection : Collection<ByteParameter> {
}
public class WordParameterCollection : Collection<WordParameter> {
}
However, what I actually want is to create a collection of Parameter<?> objects; that is, a single typed collection that can contain BooleanParameter, ByteParameter or WordParameter objects intermingled with each other and then determine which of the objects I am working with at run time.
I have tried various combinations of interfaces and base classes, but all to no avail. Is this possible, if so, can anybody help me see the light?
Thanks in advance.
Martin.
Consider the following code ...
using System;
public class Parameter<T> {
private T value;
public Parameter() {
}
public Parameter(T value)
: this() {
this.value = value;
}
public T Value {
get { return this.value; }
set { this.value = value; }
}
}
public class BooleanParameter : Parameter<Boolean> {
}
public class ByteParameter : Parameter<Byte> {
}
public class WordParameter : Parameter<UInt16> {
}
As you can see, this is a simple example where I am creating generic types that will all expose a 'Value' property, but each of the 'Value' properties will have a different data type.
If I want to create collections for these types, again this is fairly basic ...
using System.Collections.ObjectModel;
public class BooleanParameterCollection : Collection<BooleanParameter> {
}
public class ByteParameterCollection : Collection<ByteParameter> {
}
public class WordParameterCollection : Collection<WordParameter> {
}
However, what I actually want is to create a collection of Parameter<?> objects; that is, a single typed collection that can contain BooleanParameter, ByteParameter or WordParameter objects intermingled with each other and then determine which of the objects I am working with at run time.
I have tried various combinations of interfaces and base classes, but all to no avail. Is this possible, if so, can anybody help me see the light?
Thanks in advance.
Martin.