I’ve been researching how to successfully convert C++ pointer-to-member to C# but I haven’t found anything useful yet. Let’s say I have this function.
typedef int STRUCT::*DEFINED;
protected static Method(STRUCT* sampleStruct, DEFINED pMember)
{
return (sampleStruct->*pMember);
}
I have learned through my research that ->* is a pointer-to-member. In this case, we send a pointer of a member variable in a struct called STRUCT. Since Method isn’t really sure which member was sent as a parameter, it accesses it through pointer-to-member sampleStruct->*pMember.
I think Reflection could help to convert this code to C#, or maybe Delegates, but I really don’t know how to implement it, and I haven’t found any similar example online. Any help will be appreciated.
Thanks,
YT
UPDATE
This is how I have implemented this in C#.
Instead of a struct, I created an enum, and a class to represent the C++ struct, like this:
C++ struct
public struct ServerStats
{
int serverStat1;
int serverStat2;
int serverStat3;
int serverStat4;
int serverStat5;
}
Now, in C#:
public enum ServerStatsEnum
{
serverStat1,
serverStat2,
serverStat3,
serverStat4,
serverStat5,
}
public class ServerStats
{
public int[] serverStatsArray;
public ServerStats()
{
int numElementsInEnum = Enum.GetNames(typeof(ServerStatsEnum)).Length;
serverStatsArray = new int[numElementsInEnum];
}
}
}
Now, I can access the elements of the array by calling the specific enum, like this:
public static void Operation(ServerStats server1, ServerStats server2, ServerStatsEnum index)
{
Console.WriteLine("serverStatsArray[{0}] in server1 is {1}", index, server1.serverStatsArray[(int)index]);
Console.WriteLine("serverStatsArray[{0}] in server2 is {1}", index, server2.serverStatsArray[(int)index]);
}
It’s more code, but it works natively in C# and it’s more efficient than other solutions.
You can use delegates to emulate pointer-to-member access.