Skip to content

[Suggestion]Add generics to the WriteMemory method. #178

@Youcheng-cat

Description

@Youcheng-cat

Consider adding generics to the WriteMemory method to increase the clarity of the code, similar to the following code:

public bool WriteMemory<T>(UIntPtr address, T writeValue, bool removeProtection = false, Encoding encoding = null)
{
    byte[] memory;

    switch (writeValue)
    {
        case float floatValue:
            memory = BitConverter.GetBytes(floatValue);
            break;
        case int intValue:
            memory = BitConverter.GetBytes(intValue);
            break;
        case uint uintValue:
            memory = BitConverter.GetBytes(uintValue);
            break;
        case byte byteValue:
            memory = new byte[] { byteValue };
            break;
        case short shortValue: // 2byte
            memory = BitConverter.GetBytes(shortValue);
            break;
        case double doubleValue:
            memory = BitConverter.GetBytes(doubleValue);
            break;
        case long longValue:
            memory = BitConverter.GetBytes(longValue);
            break;
        case UIntPtr UIntPtrValue:
            if (MemProc.Is64Bit)
                memory = BitConverter.GetBytes((ulong)UIntPtrValue);
            else
                memory = BitConverter.GetBytes((uint)UIntPtrValue);
            break;
        case string stringValue:
            return WriteString(address, stringValue, encoding, removeProtection);
        case byte[] bytesValue:
            return WriteBytes(address, bytesValue, removeProtection);
        default:
            throw new ArgumentException($"Type \"{typeof(T).Name}\" is not support.");
    }

This is a partial example, where some methods do not actually exist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions