Make only one method of array of any kind to IntPtr

Viewed 895

Having a method to convert double[] d to IntPtr like:

public static IntPtr DoubleArrayToIntPtr(double[] d)
{
   IntPtr p = Marshal.AllocCoTaskMem(sizeof(double) * d.Length);
   Marshal.Copy(d, 0, p, d.Length);
   return p;
}

What would be the best way to make int[], float[], etc. I was thinking on making one method for each type like adding int[]:

public static IntPtr IntArrayToIntPtr(int[] d)
{
   IntPtr p = Marshal.AllocCoTaskMem(sizeof(int) * d.Length);
   Marshal.Copy(d, 0, p, d.Length);
   return p;
}

1. Could this method be generalized, if so how?

2. Is it possible to get pointer in only one line of code (As Marshal is void method)?

2 Answers
Related