简体   繁体   中英

Getting properties of derived object from base object list

I'm looking for simple way to get properties from my derived class, which inherit abstract class.

My code:

public abstract class PointLayer
{
    public abstract Type Type { get;}
}
public class PointLayer<T> : PointLayer
{
    #region PROPERTIES
    public override Type Type { get; }
    public string MapName { get; private set; }
    public MapType MapType { get; private set; }
    public Point<T> Point { get; private set; }
    #endregion

    #region C'TOR
    /// <summary>
    /// Constructor creating PointLayer
    /// </summary>
    /// <param name="mapName">Map name</param>
    /// <param name="mapType">Map type</param>
    /// <param name="point">Point</param>
    public PointLayer(string mapName, MapType mapType, Point<T> point)
    {
        MapName = mapName;
        Point = point;
        MapType = mapType;
    }
    #endregion
}

And I try to get ie MapName from elements of Layers list.

Layers = new List<PointLayer>();

In Layers = new List<PointLayer>();

Layers is a list of the parent class. You can't access child properties from the parent class, as you have no guarantee that an object of type PointLayer has property MapName.

If you are sure the objects are actually of type PointLayer you can cast them:

    var GenericLayers = new List<PointLayer<int>>();
    List<PointLayer> Layers = GenericLayers.Select(x => (PointLayer)x).ToList();

    List<int> MapNames = Layers.Select(x => ((PointLayer<int>)x).MapName).ToList();

EDIT:

Given your answer that you want a list of different types of PointLayer you should simply use a list of PointLayer:

public abstract class PointLayer
{
    public abstract Type Type { get;}
    public abstract string MapName { get; protected set; }
    public abstract MapType MapType { get; protected set; }
    public abstract Point Point { get; protected set; }
}
public class PointLayer<T> : PointLayer
{
    #region PROPERTIES
    public override Type Type { get; }
    public override string MapName { get; protected set; }
    public override MapType MapType { get; protected set; }
    public override Point Point { get; protected set; }
    #endregion

    #region C'TOR
    /// <summary>
    /// Constructor creating PointLayer
    /// </summary>
    /// <param name="mapName">Map name</param>
    /// <param name="mapType">Map type</param>
    /// <param name="point">Point</param>
    public PointLayer(string mapName, MapType mapType, Point<T> point)
    {
        Type=typeof(T);
        MapName = mapName;
        Point = point;
        MapType = mapType;
    }
    #endregion
}

Then

Layers = new List<PointLayer>();
var MapNames=Layers.Select(x=>x.MapName);

Inherit Point<T> from point and I think this is the best you can do. Depending on what functionality Point<T> has over Point , this may be sufficient. Although you will not be able to tell what is the type parameter of point, since you only know this at runtime there is no way around this. If Point is abstract, and has all the methods included in Point<T> very little difference will be made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM