C# 从泛型类强制转换为类型
我有一个泛型类,如下所示:C# 从泛型类强制转换为类型,c#,generics,C#,Generics,我有一个泛型类,如下所示: public interface IStationProperty { int Id { get; set; } string Desc { get; set; } object Value { get; } Type ValueType { get; } } [Serializable] public class StationProp<T> : IStationProperty
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); }
}
var correctlyTypedVariable = (prop.ValueType) prop.Value;
因此,在我的代码中,我有一个从db中提取值(作为字符串)的循环,这里我想做一个类型转换(在左侧),这样我可以进行可靠的值比较
我想要这样的东西:
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); }
}
var correctlyTypedVariable = (prop.ValueType) prop.Value;
我知道这类事情必须是可能的。你已经做到了
public T Value { get; set; }
返回类型化的值。
如果在以下代码中,则prop对象的类型为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; }
}
公共接口IStationProperty
{
int Id{get;set;}
字符串Desc{get;set;}
类型ValueType{get;}
}
公共接口IStationProperty:IStationProperty
{
T值{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();
}
}
公共接口IStationProperty:IComparable
{
int Id{get;set;}
字符串Desc{get;set;}
对象值{get;}
类型ValueType{get;}
}
[可序列化]
公共类StationProp:IStationProperty,其中T:IComparable
{
公共站点prop()
{
}
public StationProp(int-id,T-val,string-desc=“”)
{
Id=Id;
Desc=Desc;
值=val;
}
公共int Id{get;set;}
公共字符串Desc{get;set;}
公共T值{get;set;}
对象IStationProperty.Value
{
获取{返回值;}
}
公共类型ValueType
{
获取{return typeof(T);}
}
公共int比较(IStationProperty其他)
{
if(other.ValueType==typeof(string))
{
返回Value.CompareTo((字符串)other.Value);
}
else if(other.ValueType==typeof(int))
{
返回值.CompareTo((int)other.Value);
}
else if(other.ValueType==typeof(double))
{
返回值.CompareTo((双精度)其他.Value);
}
抛出新的NotSupportedException();
}
}
如果你能解释你打算用正确的类型变量做什么,回答起来会很容易。一些解释或伪代码?这是解决方案吗?例如,var correctlyTypedVariable=Convert.ChangeType(prop.Value,prop.ValueType)代码>不能那样做。如果您使用的是var,则类型必须在编译时而不是运行时清除。你只能这样做:“如果(你的对象是StationProp)”等等。我们谈论的是多少种不同的类型?