Multiple generic types in one container
Posted
by Lirik
on Stack Overflow
See other posts from Stack Overflow
or by Lirik
Published on 2010-05-22T03:24:56Z
Indexed on
2010/05/22
3:30 UTC
Read the original article
Hit count: 212
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?
© Stack Overflow or respective owner