[英]Converting C++ pointer to member to C#
I've been researching how to successfully convert C++ pointer-to-member to C# but I haven't found anything useful yet. 我一直在研究如何将C ++指针成员成功转换为C#,但是我还没有发现任何有用的东西。 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. 在这种情况下,我们在名为STRUCT的结构中发送成员变量的指针。 Since Method isn't really sure which member was sent as a parameter, it accesses it through pointer-to-member sampleStruct->*pMember. 由于Method不能真正确定将哪个成员作为参数发送,因此它通过指向成员的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. 我认为Reflection可以帮助将这段代码转换为C#或Delegates,但是我真的不知道如何实现它,并且我没有在网上找到任何类似的示例。 Any help will be appreciated. 任何帮助将不胜感激。
Thanks, YT 谢谢,YT
UPDATE UPDATE
This is how I have implemented this in C#. 这就是我在C#中实现的方式。
Instead of a struct, I created an enum, and a class to represent the C++ struct, like this: 我创建了一个枚举和一个类来表示C ++结构,而不是结构,如下所示:
C++ struct C ++结构
public struct ServerStats
{
int serverStat1;
int serverStat2;
int serverStat3;
int serverStat4;
int serverStat5;
}
Now, in C#: 现在,在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. 它是更多的代码,但是它可以在C#中原生运行,并且比其他解决方案更有效。
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);
}
}
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. 尽管C ++可以在编译时创建成员指针,但是您需要等到运行时才能获取PropertyInfo实例,并且需要将成员名称作为字符串传递来获取它。
Something like what jyoung suggests with delegates might work: 像jyoung向代表建议的事情可能会起作用:
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. 但这是一种令人费解的事情,不是惯用的,如果从更高的层次上看,可能有一种更简洁的解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.