Generic type pointers?

ok, so I want to create a generic class that will change the value of a data type. The reason I want to do this is because I can use undo and redo methods. I could write a class for every important type that I need. I.E. double, int ... but it would be much simpler if I could create a generic class for this.

This is what I have

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; } } 

this is what i want

 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; } } 

but it gives me the error Error "Unable to accept address, get size or declare a pointer to a managed type (" T ")"

Is there a better way to do this or a way around this error?

+7
generics pointers c # value-type
Jun 17 '13 at 20:23
source share
4 answers

Instead of indicating a pointer to a value, setter:

 class CommandChangeValue<T> : Command { T _previous; T _new; Action<T> _set; public CommandChangeValue(T value, Action<T> setValue, T newValue) { _previous = value; _new = newValue; _set = setValue; setValue(_new); } public void Undo() { _set(_previous); } public void Redo() { _set(_new); } } // ... double v = 42; var c = new CommandChangeValue(v, d => v = d, 99); 
+8
Jun 17 '13 at 20:28
source share

For writing purposes only, you can get a pointer to a generic type or any other type using these methods ....

  /// <summary> /// Provides the current address of the given element /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public static System.IntPtr AddressOf<T>(T t) //refember ReferenceTypes are references to the CLRHeader //where TOriginal : struct { System.TypedReference reference = __makeref(t); return *(System.IntPtr*)(&reference); } [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] static System.IntPtr AddressOfRef<T>(ref T t) //refember ReferenceTypes are references to the CLRHeader //where TOriginal : struct { System.TypedReference reference = __makeref(t); System.TypedReference* pRef = &reference; return (System.IntPtr)pRef; //(&pRef) } 

I used them along with several others to implement the slice form used with arrays.

+6
May 04 '16 at a.m.
source share

Do not use pointers.

If you use regular links and always interact with the value through a property of this class, everything will work fine.

-one
Jun 17 '13 at 20:25
source share

Do not use pointers, as said in another answer and in the comments. And if you want to have some undo / redo functions in your application, you can look in the Memento pattern .

-3
Jun 17 '13 at 20:27
source share



All Articles