我正在尝试创建一个通用类来处理浮点数和双打。我需要根据变量的类型(浮点型或双精度型)执行计算,但我不确定以下实现是否是正确的方法。需要一些建议。
// computeFloat is a method of some other class which actually computes and returns a float value
float computeFloat()
{
float a;
....
return a;
}
// setFloat is a method of some other class which actually sets a float value
void setFloat(float val)
{
.....
}
TestClass<T> : IDisposable
{
public void getValue(ref T val)
{
if(val is float)
{
object retVal = computeFloat();
val = (float)retVal;
}
else
{
throw new Exception(" Not implemented");
}
}
public void setValue(T val)
{
if(val is float)
{
object obj = val as object;
float retVal = (float)obj;
setFloat(retVal);
}
else
{
throw new Exception(" Not implemented");
}
}
}