Click here to Skip to main content
15,900,378 members

Comments by dzadel (Top 5 by date)

dzadel 25-May-22 4:39am View    
OkThanks for your help.
dzadel 24-May-22 17:49pm View    
I need to allocate a buffer based on a generic type T. if T is managed then the size of each item is the size of a refernce, otherwise T is a struct and a have to call my SizeOf<t>() method and get the size of this struct.

I nedd, for example implementing a method like:
static byte[] AllocBuffer<T>(int itemCount)
{}
dzadel 24-May-22 17:33pm View    
This method do not accept generic type.
Take a look at its impl.

public static int SizeOf<t>()
{
Type t = typeof(T);
if (t.IsGenericType)
{
throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(T));
}

return SizeOfHelper(t, throwIfNotMarshalable: true);
}
dzadel 24-May-22 17:04pm View    
here is a concret code:

static unsafe int SizeOf<T>() where T: unmanaged => sizeof(T);
static int GetSize<T>() => is T unmanaged ? SizeOf<T> : IntPtr.Size;

what construct to put in place of "is T unmanaged"?
dzadel 24-May-22 17:01pm View    
How to force the compiler to accept the call to the method with constraint?