Multiple generic types in one container
- by Lirik
I was looking at the answer of this question regarding multiple generic types in one container and I can't really get it to work: the properties of the Metadata class are not visible, since the abstract class doesn't have them. Here is a slightly modified version of the code in the original question:
public abstract class Metadata
{
}
public class Metadata<T> : Metadata
{
// ... some other meta data
public T Function{ get; set; }
}
List<Metadata> metadataObjects;
metadataObjects.Add(new Metadata<Func<double,double>>());
metadataObjects.Add(new Metadata<Func<int,double>>());
metadataObjects.Add(new Metadata<Func<double,int>>());
foreach( Metadata md in metadataObjects)
{
var tmp = md.Function; // <-- Error: does not contain a definition for Function
}
The exact error is:
error CS1061: 'Metadata' does not
contain a definition for 'Function' and no
extension method 'Function' accepting a
first argument of type 'Metadata'
could be found (are you missing a
using directive or an assembly
reference?)
I believe it's because the abstract class does not define the property Function, thus the whole effort is completely useless. Is there a way that we can get the properties?