MemoryRead<;T>;只有T int,float,string&;大小为(T)

本文关键字:amp string 小为 int lt gt 只有 MemoryRead float | 更新日期: 2023-09-27 18:26:00

你好,我现在正在写Memory类,它包含MemoryRead函数。我有了第一个MemoryReadInt,但当我想到其他类型的多个函数时,比如字符串float。。太多了。一个代码的更改也需要对每个函数进行更改。

所以我考虑了类型。

但我坚持了下来。这是我的主菜。

        Memory m = new Memory("lf2");
        Console.WriteLine("Health: " + m.MemoryRead<int>(0x00458C94, 0x2FC));
        m.CloseMemoryprocess();

现在这是的功能

    public int MemoryRead<T>(int baseAdresse, params int[] offsets)
    {
        if(!ProcessExists())
            return -1;
        uint size = sizeof(T); << this cause the error
        byte[] buffer = new byte[size];

因为我需要sizeof,因为我需要定义缓冲区的大小。

此外,返回值也将很快变为T。所以我不知道我在这里能做什么。我希望你能帮助我。

顺便说一句,我现在只想使用字符串,int,float。我可以限制它吗?

MemoryRead<;T>;只有T int,float,string&;大小为(T)

我想我需要回答自己的问题。经过长时间的研究(昨天…我需要先睡觉,然后发帖:D)我改变了我的计划。

我先找类型。然后我转到下一个MemoryRead再取一个uint大小。它还检查类型。但如果客户使用uint大小和T字符串。所以函数知道他想要字符串回来。但是字符串需要用户给定的大小:D

public T MemoryRead<T>(int baseAdresse, params int[] offsets) 
{
    if(!ProcessExists())
        return default(T);
    uint size = 0;
    if(typeof(T) == typeof(int)) {
        size = sizeof(int);
        return (T)(object)MemoryRead<int>(baseAdresse, size, offsets);
    } else if(typeof(T) == typeof(float)) {
        size = sizeof(float);
        return (T)(object)MemoryRead<float>(baseAdresse, size, offsets);
    } else if(typeof(T) == typeof(double)) {
        size = sizeof(double);
        return (T)(object)MemoryRead<double>(baseAdresse, size, offsets);
    }else {
        MessageBox.Show("Wrong type. Maybe you want to use MemoryRead<string>(int, uint, params [] int)", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        throw new ArgumentException("Wrong type. Maybe you want to use MemoryRead<string>(int, uint, params [] int)");
    }
}

备选方案我也可以这样做:因为重载函数需要uint。。用户可以忘记将其转换为uint并使用int。这只会填充params:

    public int MemoryReadInt(int baseAdresse, params int[] offsets)
    {
        return MemoryRead<int>(baseAdresse, sizeof(int), offsets);
    }
    public string MemoryReadString(int baseAdresse, uint readSize, params int[] offsets)
    {
        return MemoryRead<string>(baseAdresse, readSize, offsets);
    }