访问继承的泛型类的泛型属性

时间:2018-10-03 15:10:24

标签: c# generics inheritance

有关:C# - Multiple generic types in one listAccess to property of inherited generic class

请考虑以下代码:

public abstract class Metadata
{
}

public class Metadata<T> : Metadata
{
    public List<T> MyCollection{set; get;}
}

public class MetaDataCollection
{
    public void DoSomeThing()
    {
        List<Metadata> metadataObjects = new List<Metadata>
        {
            new Metadata<int>() { MyCollection = new List<int>()},
            new Metadata<bool>() { MyCollection = new List<bool>()},
            new Metadata<double>() { MyCollection = new List<double>()},
        };

        foreach(vat item in metadataObjects)
        {
            item.MyCollection??????
        }
    }
}

在上面的代码中,我如何访问MyCollection属性?

谢谢

4 个答案:

答案 0 :(得分:1)

这不是很优雅,但是您可以查找泛型类型参数并按如下所示进行相应的转换。

public class MetaDataCollection
{
    public void DoSomeThing()
    {
        List<Metadata> metadataObjects = new List<Metadata>
        {
            new Metadata<int>() { MyCollection = new List<int>() { 1, 2, 3, 4} },
            new Metadata<bool>() { MyCollection = new List<bool>() { true, false, false} },
            new Metadata<double>() { MyCollection = new List<double>() { 1.5, 2.1 } },
        };

        Dictionary<Type, Action<Metadata>> actionLookup = new Dictionary<Type, Action<Metadata>>()
        {
            { typeof(int), (meta) => Console.WriteLine(((Metadata<int>)meta).MyCollection[0]) },
            { typeof(bool), (meta) => Console.WriteLine(((Metadata<bool>)meta).MyCollection[0]) },
            { typeof(double), (meta) => Console.WriteLine(((Metadata<double>)meta).MyCollection[0]) }
        };

        foreach (var item in metadataObjects)
        {
            Type metaDataType = item.GetType().GenericTypeArguments.First();
            actionLookup[metaDataType](item);
        }
    }
}

答案 1 :(得分:1)

您可以像List<> https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1那样修复它。

//use interfaces instead of abstract class
public interface IMetadata
{
    List<object> MyCollection { get; set; }
}

public interface IMetadata<T>
{
    List<T> MyCollection { get; set; }
}

//as List<> the List and List<> don't derive from each other but instead have a common interface the non-generic IList
public class Metadata : IMetadata
{
    public List<object> MyCollection { get; set; }
}

//implement the generic and non-generic interface
public class Metadata<T> : IMetadata<T>, IMetadata
{
    public List<T> MyCollection { get; set; }

    //hide the non-generic interface member MyCollection
    List<object> IMetadata.MyCollection { get { return this.MyCollection.Cast<object>().ToList(); } set { this.MyCollection = value.Cast<T>().ToList(); } }

}

public class MetaDataCollection
{
    public void DoSomeThing()
    {
        //make a list of IMetadata
        List<IMetadata> metadataObjects = new List<IMetadata>
        {
            new Metadata<int>() { MyCollection = new List<int>()},
            new Metadata<bool>() { MyCollection = new List<bool>()},
            new Metadata<double>() { MyCollection = new List<double>()},
        };

        foreach (var item in metadataObjects)
        {
            //item.MyCollection is now a List<object>
        }
    }
}

答案 2 :(得分:0)

下面的代码打破了OOP的思想和规则,但可以达到目的。 提防异常

trueObj

答案 3 :(得分:0)

您必须检查Item类型,然后将其转换为合适的MetaData:

public abstract class Metadata
{
}

public class Metadata<T> : Metadata
{
    public List<T> MyCollection { set; get; }
}

public class MetaDataCollection
{
    public void DoSomeThing()
    {
        List<Metadata> metadataObjects = new List<Metadata>
    {
        new Metadata<int>() { MyCollection = new List<int>()},
        new Metadata<bool>() { MyCollection = new List<bool>()},
        new Metadata<double>() { MyCollection = new List<double>()},
    };

        foreach (var item in metadataObjects)
        {
            if (item is Metadata<int>)
            {
                var intItem = item as Metadata<int>;
                intItem.Add(10);
            }
        }
    }
}