Converting C++ pointer to member to C#
-
17-06-2021 - |
Question
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.
Solution
You can use delegates to emulate pointer-to-member access.
class C
{
public int Method1() { return 1; }
public int Method2() { return 2; }
}
class Program
{
static void Main(string[] args)
{
C myC = new C();
Func<C, int> ptrToMember1 = (C c) => { return c.Method1(); };
int i = Method(myC, ptrToMember1 );
}
static int Method(C c, Func<C, int> method)
{
return method(c);
}
}
OTHER TIPS
From an implementation standpoint, reflection can do something very similar:
protected static int Method(MyType obj, PropertyInfo member)
{
return member.GetValue(obj, null);
}
The big difference is on the calling side. Whereas C++ can create a member pointer at compile time, you need to wait until run time to get a PropertyInfo instance, and you need to pass in the member name as a string to get it.
Something like what jyoung suggests with delegates might work:
class Foo
{
private int m_val;
public Func<int> GetValGetter()
{
return () => { return m_val; };
}
}
But this is kind of convoluted and not idiomatic, and there's probably a cleaner way to solve it if you look at it from a higher level.