Compare byte [] to T

I want to make a list of pointers to locations that contain a specific value in the process memory of another process. The value can be short, int, long, string, bool, or something else.

My idea is to use Generics for this. I have one problem with creating it, how can I tell the compiler what type it needs to convert byte array to?

This is what I did:

    public List<IntPtr> ScanProccessFor<T>(T ItemToScanFor)
    {
        List<IntPtr> Output = new List<IntPtr>();

        IntPtr StartOffset = SelectedProcess.MainModule.BaseAddress;
        int ScanSize = SelectedProcess.MainModule.ModuleMemorySize;

        for (int i = 0; i < ScanSize; i++)
            if (ReadMemory(SelectedProcess, StartOffset + i, (UInt16)Marshal.SizeOf(ItemToScanFor)) == ItemToScanFor)
                Output.Insert(Output.Count,StartOffset + i);

        return Output;
    }

How can I tell the compiler that it needs to convert byte [] to enter T?

+5
source share
2 answers

Your question is a bit confusing, but I will try to answer that I can

Instead of taking a generic type, I would probably write a method that takes an instance of an interface, such as IConvertableToByteArrayor something like that.

public IConvertableToByteArray
{    
    public byte[] ToByteArray();
}

,

public IntConvertableToByteArray : IConvertableToByteArray
{
    public int Value{get; set;}

    public byte[] ToByteArray()
    {
        insert logic here
    }
}
+2

Marshal.StructureToPtr, ( "" ). , case.

, - 1 , 4 8 .

+1

All Articles