I would like to know if there is any way to create a generic pointer in C#. The problem is quite similar to Pointers of generic type?, I can however not see any way to make any of the solutions work in my case. Although it helped explain why limiting the generic type to be a value type wasn't enough, Why is pointer to generic types not allowed?, did also not help as creating overloads for every possible type, while possible, is not desired here.
Thus the problem setting is basically that there is some generic function that needs to cast a void*
to a specific type of pointer, example function see below:
public static unsafe T[] fooBar<T>(void* ptr, int length) where T : struct
{
T* typedPointer = (T*) ptr; // This does not appear to be possible in this form
T[] retVal = new T[length];
for (int i = 0; i < length; i++) {
T[i] = *typedPointer;
++typedPointer;
}
return retVal;
}
The reason that the use of pointers is necessary here is due to some unmanaged code with C-style exports being called, which returns a System.Reflection.Pointer
, which is the turned into a void*
via Pointer.Unbox()
.
Is there any way at all to cast void*
to T*
as seen above, or is the use of a generic type pointer impossible?
Aucun commentaire:
Enregistrer un commentaire