Generic Assignment Operation

60 Views Asked by At

I am currently writing my own C# Memory editing Library.

I am trying to replicate the As function from the C++ class below in C#.

class globalHandle
{
private:
    void* _handle;
public:

    globalHandle(int index)
        : _handle(&m_globalPtr[index >> 18 & 0x3F][index & 0x3FFFF])
    { }

    globalHandle(void* p)
        : _handle(p)
    { }

    globalHandle(const globalHandle& copy)
        : _handle(copy._handle)
    { }

    globalHandle At(int index)
    {
        return globalHandle(reinterpret_cast<void**>(this->_handle) + (index));
    }

    globalHandle At(int index, int size)
    {
        // Position 0 = Array Size
        return this->At(1 + (index * size));
    }

    template <typename T>
    T* Get()
    {
        return reinterpret_cast<T*>(this->_handle);
    }

    template <typename T>
    T& As()
    {
        return *this->Get<T>();
    }
};

It looks like the following:

globalHandle(1573972).As<int>() = 123;

I want to archive a function in C# like this:

m.memory(pointer, offset).Set<int>() = 123;

My Memory function looks like this:

long address;
public Memory(long pointer, long offset)
{
    byte[] buffer_temp = new byte[16];

    ReadProcessMemory(procHandle, (long)BaseAddress + pointer, buffer_temp, buffer_temp.Length, IntPtr.Zero);
    address = BitConverter.ToInt64(buffer_temp, 0) + offset;
}

I tried Generics and dynamic but couldnt get it working.

Do you Guys have any Idea how i can archive what i want?

0

There are 0 best solutions below