Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics problem

Tags:

c#

generics

I'm making a class so I can cheat at minesweeper, and is currently building up some generics... but I'm kinda stuck. I want to return a int but how do i convert it?

public T ReadMemory<T>(uint adr)
{
    if( address != int.MinValue )
        if( typeof(T) == typeof(int) )
            return Convert.ChangeType(MemoryReader.ReadInt(adr), typeof(T));
        else
            MessageBox.Show("Unknown read type");
}
like image 504
Jason94 Avatar asked Jul 01 '26 13:07

Jason94


2 Answers

You need to cast the return value from the call to ChangeType

return (T)Convert.ChangeType(MemoryReader.ReadInt(adr), typeof(T)); 
like image 126
Chris Taylor Avatar answered Jul 03 '26 04:07

Chris Taylor


Try casting:

return (T)Convert.ChangeType(MemoryReader.ReadInt(adr), typeof(T));
like image 27
Darin Dimitrov Avatar answered Jul 03 '26 04:07

Darin Dimitrov