如何获取列表中通用 class 的属性?
How to get to the properties of a generic class in a list?
我有一个像这样的通用 class:
public class StationProperty<T> : StationProperty
{
public StationProperty()
{
}
public StationProperty(int id, T val, string desc = "")
{
Id = id;
Desc = desc;
Value = val;
}
public int Id { get; set; }
public string Desc { get; set; }
public T Value { get; set; }
}
注意继承,我稍后会解释,但是抽象 class 看起来像这样:
public interface StationProperty
{
}
如您所见,没有什么特别之处 - 也没有明确的属性。
感谢这种机制,我可以像这样传递这些项目的列表:
var props = new List<StationProperty>();
props.Add(new StationProperty<bool>(39, true));
props.Add(new StationProperty<int>(41, 1));
到目前为止一切顺利,但现在我希望能够做到:
Foreach(var prop in props)
{
//prop.Id
//prop.Desc
//and most importantly prop.Value.GetType or prop.GetType
}
而是缺少这些属性:
如果我手动将属性添加到抽象中 class 那么我可以解决 Id 和 Desc,但我很可能需要为 Value 添加一个对象类型,这将否定使用泛型的原因首先。
所以我的问题是,我想做的能做吗?我哪里错了。
对于 Id
和 Desc
属性,获取它们的最简单方法是将它们添加到界面。
或者在您的示例中,使用(抽象的)class 而不是界面并将属性放在那里可能会更好。
使用这种方法,您不必在通用 class.
中实现它们
要在循环中获取值 属性,您需要使用反射:
var val = prop.GetType().GetProperty("Value").GetValue(prop);
这条语句应该可以解决问题。
不过,正如@Steve Mitcham 在评论中所说,如果您描述了您原来的问题,也许会有更好的解决方案。
您是否在寻找如下代码?
您始终可以获得类型,但在使用接口时只能将值读取为 'object',泛型 class 也可以获得强类型值并允许您设置。您还可以允许通过接口设置 Value 并在类型不正确时抛出异常。
public class StationProperty<T> : StationProperty
{
public StationProperty()
{
}
public StationProperty(int id, T val, string desc = "")
{
Id = id;
Desc = desc;
Value = val;
}
public int Id { get; set; }
public string Desc { get; set; }
public T Value { get; set; }
object StationProperty.Value
{
get { return Value; }
}
public Type ValueType
{
get { return typeof (T); }
}
}
public interface StationProperty
{
int Id { get; set; }
string Desc { get; set; }
object Value { get; }
Type ValueType { get; }
}
我有一个像这样的通用 class:
public class StationProperty<T> : StationProperty
{
public StationProperty()
{
}
public StationProperty(int id, T val, string desc = "")
{
Id = id;
Desc = desc;
Value = val;
}
public int Id { get; set; }
public string Desc { get; set; }
public T Value { get; set; }
}
注意继承,我稍后会解释,但是抽象 class 看起来像这样:
public interface StationProperty
{
}
如您所见,没有什么特别之处 - 也没有明确的属性。
感谢这种机制,我可以像这样传递这些项目的列表:
var props = new List<StationProperty>();
props.Add(new StationProperty<bool>(39, true));
props.Add(new StationProperty<int>(41, 1));
到目前为止一切顺利,但现在我希望能够做到:
Foreach(var prop in props)
{
//prop.Id
//prop.Desc
//and most importantly prop.Value.GetType or prop.GetType
}
而是缺少这些属性:
如果我手动将属性添加到抽象中 class 那么我可以解决 Id 和 Desc,但我很可能需要为 Value 添加一个对象类型,这将否定使用泛型的原因首先。
所以我的问题是,我想做的能做吗?我哪里错了。
对于 Id
和 Desc
属性,获取它们的最简单方法是将它们添加到界面。
或者在您的示例中,使用(抽象的)class 而不是界面并将属性放在那里可能会更好。 使用这种方法,您不必在通用 class.
中实现它们要在循环中获取值 属性,您需要使用反射:
var val = prop.GetType().GetProperty("Value").GetValue(prop);
这条语句应该可以解决问题。
不过,正如@Steve Mitcham 在评论中所说,如果您描述了您原来的问题,也许会有更好的解决方案。
您是否在寻找如下代码? 您始终可以获得类型,但在使用接口时只能将值读取为 'object',泛型 class 也可以获得强类型值并允许您设置。您还可以允许通过接口设置 Value 并在类型不正确时抛出异常。
public class StationProperty<T> : StationProperty
{
public StationProperty()
{
}
public StationProperty(int id, T val, string desc = "")
{
Id = id;
Desc = desc;
Value = val;
}
public int Id { get; set; }
public string Desc { get; set; }
public T Value { get; set; }
object StationProperty.Value
{
get { return Value; }
}
public Type ValueType
{
get { return typeof (T); }
}
}
public interface StationProperty
{
int Id { get; set; }
string Desc { get; set; }
object Value { get; }
Type ValueType { get; }
}