This is probably not possible, but I have this class:
public class Metadata<DataType> where DataType : struct
{
private DataType mDataType;
}
There's more to it, but let's keep it simple. The generic type (DataType) is limited to value types by the where statement. What I want to do is have a list of these Metadata objects of varying types (DataType). Such as:
List<Metadata> metadataObjects;
metadataObjects.Add(new Metadata<int>());
metadataObjects.Add(new Metadata<bool>());
metadataObjects.Add(new Metadata<double>());
Is this even possible?
I have also used a non-generic version, using the
new
keyword:Explicit interface implementation is used to allow both
Data
members:You could derive a version targeting value types:
This can be extended to any kind of generic constraints.
Following leppie's answer, why not make
MetaData
an interface: