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?
List<object>? They won't stop boxing/unboxing, they won't remove the need for casting, and ultimately, you are getting aMetadataobject that does not tell you anything about the actualDataType, I was searching for a solution to address those issues. If you're going to declare an interface/class, just for the sake of being able to put the implementing/derived generic type in a generic list, just how different is that than using aList<object>other than having a meaningless layer?List<Metadata<object>>does the trick.structconstraint, it doesn't work here. SeeMetaDatareference types instead of your original value types with no (compile time) information about the underlying value type of each element, that's effectively "boxing".