好的,所以我想创建一个通用类来更改数据类型的值。我想这样做的原因是我可以有撤消和重做方法。我可以为我需要的每个值类型编写一个类。IE double, int ...但如果我可以创建一个通用类来执行此操作会容易得多。
这就是我所拥有的
class CommandChangeDouble : Command
{
double _previous;
double _new;
double* _objectRef;
public unsafe CommandChangeDouble(double* o, double to)
{
_objectRef = o;
_previous = *o;
_new = to;
*_objectRef = _new;
}
public unsafe void Undo()
{
*_objectRef = _previous;
}
public unsafe void Redo()
{
*_objectRef = _new;
}
}
这就是我要的
class CommandChangeValue<T> : Command
{
T _previous;
T _new;
T* _objectRef;
public unsafe CommandChangeValue(T* o, T to)
{
_objectRef = o;
_previous = *o;
_new = to;
*_objectRef = _new;
}
public unsafe void Undo()
{
*_objectRef = _previous;
}
public unsafe void Redo()
{
*_objectRef = _new;
}
}
但这给了我错误错误“无法获取地址、获取大小或声明指向托管类型 ('T') 的指针”
有没有更好的方法来做到这一点或解决这个错误的方法?