-1

So if i have code like this:

public static void GetPointer(int[] array)
{
    fixed(void* pointer = array)
    {

    }
}

I can take a pointer of unknown type of an int array very easy. But what if i want to take a pointer of an array of unknown type? How can i do it?

public static void GetPointer(Array array)
{
    fixed(void* pointer = array)
    {

    }
}

Doesn't seems to work.

Eugen1344
  • 165
  • 3
  • 17
  • 3
    Can we find out why you need to use pointers? In my 12 years of working in .NET, I think I've only had to use them once, with unmanaged code. It will help knowing what you need to do with the pointer if we know how you need to use it. – Ron Beyer Apr 28 '15 at 14:00
  • 1
    I am writing a wrapper to unmanaged DLL. The best way to do it is to use unmanaged code. I am not quite newbie, so i know that it IS the best way – Eugen1344 Apr 28 '15 at 14:07
  • So the Array being passed in is from the DLL? And the DLL passes in a void* for the array? – Ron Beyer Apr 28 '15 at 14:08
  • @Eugen1344 Whenever I have had to wrap an unmanged DLL, I have had access to the source of said unmanaged DLL, and I have written the wrapper on the unmanaged side, making sure the methods only take primitive types to remove the need for marshalling when using PInvoke. Any time a pointer is needed, I just used IntPtr, and used the pointer returned to me from the unmanged code. – David Watts Apr 28 '15 at 14:09
  • Using the intptr will drop down the performance, i don't want to use it. And even if i will use it, i will have need to cast Array to IntPtr. I want to method to take primitive arrays as parameters and i don't want them to take IntPtr as parameter – Eugen1344 Apr 28 '15 at 14:11
  • People, don't ask me please what am i doing wrong. Just answer the question please, i know what i am doing :) – Eugen1344 Apr 28 '15 at 14:12
  • @Eugen1344 *I am not quite a newbie, so I know that it **IS** the best way.* Well, your arrogance isn't humbling when your asking for help. – Greg Apr 28 '15 at 14:13
  • @Gred I am jusk asking you to answer the question what i asked here – Eugen1344 Apr 28 '15 at 14:14
  • 1
    How can we answer the question if we can't dig deeper into what you are trying to do? Maybe you could post the method signature? If you want to take any type of primitive array, make an overloaded method for every primitive (there aren't that many). – Ron Beyer Apr 28 '15 at 14:15
  • I know that i can make tonns of overloads. That will be +100-200 functions , so this code will be bad. The best way is to take the pointer of an Array, i am just looking for a way to do it. Is it so complicated? – Eugen1344 Apr 28 '15 at 14:19
  • Yes, because you can't take a pointer to `Array`, so we need to figure out something else but you're fixated on a solution that doesn't exist. – Ron Beyer Apr 28 '15 at 14:23
  • @Greg, i just didn't include unsafe keyword here. Ron Beyer that is what i wanted to hear! If solution doen't exist completely, i will figure out how to do it by myself, thanks for answers! – Eugen1344 Apr 28 '15 at 14:29
  • Here is similar thread: https://social.msdn.microsoft.com/Forums/vstudio/en-US/78c3a0dd-b461-47cf-bc5d-9c14e1bf848e/systemarray-to-pointer –  Apr 28 '15 at 14:34
  • @Eugen1344 You sure, because the `fixed` context can only be used within an `unsafe` context. Which you haven't clearly defined. Your also trying to take the value of an already fixed expression, which is incorrect. So if you provide better context you'll get a better answer. – Greg Apr 28 '15 at 14:35
  • @Greg the code is 1000+ lines. And in top is my class declaration wirtten like: "public static unsafe class MyClass" – Eugen1344 Apr 28 '15 at 14:42

1 Answers1

1

Your intent for the pointer is foreign to me, however you could make use of Generics. These will allow you to use different types.

public static void GetPointer<T>(T[] array)
{
     // Do Stuff...
}

You would specify your type in the <T> which will allow that type to now be assigned to T which you will use in your method. Not sure why you need pointers, the beauty of .Net is to alleviate the need for them.

Especially since a pointer would be considered unmanaged code in .Net. Which you normally don't use.

Greg
  • 11,302
  • 2
  • 48
  • 79
  • This is not working and this is not the best way to do it cause user can specify something that is NOT an array – Eugen1344 Apr 28 '15 at 14:08
  • @Eugen1344 The answer does work, maybe if you provided more information you would get a better answer. – Greg Apr 28 '15 at 14:16
  • @Greg i did outline it in class declaration. And your aswer is still incorrect, sorry – Eugen1344 Apr 28 '15 at 14:31