从泛型 class 转换为类型
Cast to a type from a generic class
我有一个通用的 class,看起来像这样:
public interface IStationProperty
{
int Id { get; set; }
string Desc { get; set; }
object Value { get; }
Type ValueType { get; }
}
[Serializable]
public class StationProp<T> : IStationProperty
{
public StationProp()
{
}
public StationProp(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 IStationProperty.Value
{
get { return Value; }
}
public Type ValueType
{
get { return typeof(T); }
}
得到类型的属性是:
public Type ValueType
{
get { return typeof(T); }
}
所以在我的代码中,我有一个循环从数据库中提取值(作为字符串),在这里我想进行类型转换(在左侧)以便进行可靠的值比较。
我想要这样的东西:
var correctlyTypedVariable = (prop.ValueType) prop.Value;
我知道这种事情必须是可能的。
您已经
public T Value { get; set; }
其中 returns 输入的值。
如果在下面的代码中,道具对象的类型是 IStationProperty
var correctlyTypedVariable = (prop.ValueType) prop.Value;
那么也许您的问题出在界面上:您最好使用通用界面:
public interface IStationProperty
{
int Id { get; set; }
string Desc { get; set; }
Type ValueType { get; }
}
public interface IStationProperty<T> : IStationProperty
{
T Value { get; }
}
与 IComparable:
public interface IStationProperty : IComparable<IStationProperty>
{
int Id { get; set; }
string Desc { get; set; }
object Value { get; }
Type ValueType { get; }
}
[Serializable]
public class StationProp<T> : IStationProperty where T : IComparable
{
public StationProp()
{
}
public StationProp(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 IStationProperty.Value
{
get { return Value; }
}
public Type ValueType
{
get { return typeof(T); }
}
public int CompareTo(IStationProperty other)
{
if (other.ValueType == typeof(string))
{
return Value.CompareTo((string)other.Value);
}
else if (other.ValueType == typeof(int))
{
return Value.CompareTo((int)other.Value);
}
else if (other.ValueType == typeof(double))
{
return Value.CompareTo((double)other.Value);
}
throw new NotSupportedException();
}
}
我有一个通用的 class,看起来像这样:
public interface IStationProperty
{
int Id { get; set; }
string Desc { get; set; }
object Value { get; }
Type ValueType { get; }
}
[Serializable]
public class StationProp<T> : IStationProperty
{
public StationProp()
{
}
public StationProp(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 IStationProperty.Value
{
get { return Value; }
}
public Type ValueType
{
get { return typeof(T); }
}
得到类型的属性是:
public Type ValueType
{
get { return typeof(T); }
}
所以在我的代码中,我有一个循环从数据库中提取值(作为字符串),在这里我想进行类型转换(在左侧)以便进行可靠的值比较。
我想要这样的东西:
var correctlyTypedVariable = (prop.ValueType) prop.Value;
我知道这种事情必须是可能的。
您已经
public T Value { get; set; }
其中 returns 输入的值。 如果在下面的代码中,道具对象的类型是 IStationProperty
var correctlyTypedVariable = (prop.ValueType) prop.Value;
那么也许您的问题出在界面上:您最好使用通用界面:
public interface IStationProperty
{
int Id { get; set; }
string Desc { get; set; }
Type ValueType { get; }
}
public interface IStationProperty<T> : IStationProperty
{
T Value { get; }
}
与 IComparable:
public interface IStationProperty : IComparable<IStationProperty>
{
int Id { get; set; }
string Desc { get; set; }
object Value { get; }
Type ValueType { get; }
}
[Serializable]
public class StationProp<T> : IStationProperty where T : IComparable
{
public StationProp()
{
}
public StationProp(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 IStationProperty.Value
{
get { return Value; }
}
public Type ValueType
{
get { return typeof(T); }
}
public int CompareTo(IStationProperty other)
{
if (other.ValueType == typeof(string))
{
return Value.CompareTo((string)other.Value);
}
else if (other.ValueType == typeof(int))
{
return Value.CompareTo((int)other.Value);
}
else if (other.ValueType == typeof(double))
{
return Value.CompareTo((double)other.Value);
}
throw new NotSupportedException();
}
}