0

I'm trying to do a little "memcpy" hack in C#. I keep getting stuck on this part, because it won't convert System.Type to byte*

    public unsafe void memcpy(byte* dest, object src, int length)
    {
        byte* nsrc;
        byte* ndst;
        nsrc = (byte*)((src.GetType())src);

    }

As you see I try to get the type of the object, and then cast it to the original object.

Any ideas?

Update:

Maybe using serialization?

    private byte[] ObjectToByteArray(Object obj)
    {
        if(obj == null)
            return null;
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        bf.Serialize(ms, obj);
        return ms.ToArray();
    }

    private void memcpy(byte[] dest, int pos, object src, int len)
    {
        byte[] ba = ObjectToByteArray(src);
        Array.Copy(ObjectToByteArray(src), 0, dest, pos, len);
    }
user1594121
  • 107
  • 1
  • 8
  • What good would that do to you? A cast is only useful when you know the type at compile time... Anyway, what are you trying to accomplish? What do you expect the type of src to be ? – Thomas Levesque Jul 06 '13 at 15:43
  • C# is not C/C++ you can't just cast things willy nilly. It is a typesafe language, a reference cannot be cast to a pointer. – Mgetz Jul 06 '13 at 15:43
  • I assume this is what you're trying to do: http://stackoverflow.com/questions/14141172/getting-a-byte-array-from-a-pinned-object – Douglas Jul 06 '13 at 15:45
  • @ThomasLevesque I expect the type to be multiple values at compile time. – user1594121 Jul 06 '13 at 15:45
  • @user1594121, that won't work... anything that is convertible to a pointer has to be a pointer, and a pointer is not an object, so such a cast could never work. – Thomas Levesque Jul 06 '13 at 15:46
  • @ThomasLevesque I updated, maybe that'll work? – user1594121 Jul 06 '13 at 15:52
  • @user1594121 what is your goal here? Are you trying to assemble a buffer? Because there are **MUCH** better ways. – Mgetz Jul 06 '13 at 15:57
  • You seem to confuse compile-time types and runtime types and instances of `System.Type`. An expression like `(src.GetType())src` is not legal, and the intention behind it does not make sense, really. – Jeppe Stig Nielsen Jul 06 '13 at 16:01
  • @Mgetz yes and no, I want to use address's, it looks nicer, it's faster, and cleaner. also maybe I should serialize, then use pointers to copy the data? – user1594121 Jul 06 '13 at 16:16
  • 1
    @user1594121 just use a `MemoryStream` and then use [`BitConverter.GetBytes()`](http://msdn.microsoft.com/en-us/library/de8fssa4.aspx) messing around with pointers in C# is a great way to shoot yourself in the foot, particularly when the functionality to do what you're intending is already built into the framework and does not require `unsafe`. – Mgetz Jul 06 '13 at 16:23
  • what do you effectively want to do with the object variable?...get its value?..other thing? – terrybozzio Jul 06 '13 at 16:42

2 Answers2

3

Not clear what you want, but maybe this is helpful?

int yourInt32 = ...;
byte[] bitsFromInt32Value = BitConverter.GetBytes(yourInt32);

long yourInt64 = ...;
byte[] bitsFromInt64Value = BitConverter.GetBytes(yourInt64);
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
0

taken from msdn:

Pointer types do not inherit from object and no conversions exist between pointer types and object. Also, boxing and unboxing do not support pointers. However, you can convert between different pointer types and between pointer types and integral types.

link:http://msdn.microsoft.com/en-us/library/y31yhkeb(v=vs.100).aspx

            int number = 200;

            unsafe
            {
                // Convert to byte:
                byte* p = (byte*)&number;
            }
terrybozzio
  • 4,424
  • 1
  • 19
  • 25