I have a generic class that looks like this:
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); }
}
The property that gets the type is:
public Type ValueType
{
get { return typeof(T); }
}
So in my code, I have a loop pulling values (as string) from the db, here I want to do a type conversion (on the left side) so I can do a reliable value comparison.
I would like something like this:
var correctlyTypedVariable = (prop.ValueType) prop.Value;
I know this kind of thing has to be possible.
You already have
public T Value { get; set; }
which returns the typed value. If on the following code, the prop object is of type IStationProperty
var correctlyTypedVariable = (prop.ValueType) prop.Value;
then maybe your problem is on the interface : you should better use a generic one :
public interface IStationProperty
{
int Id { get; set; }
string Desc { get; set; }
Type ValueType { get; }
}
public interface IStationProperty<T> : IStationProperty
{
T Value { get; }
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다