访问 C++通过反射从 C# 获取非成员函数
我需要获得一些有关 C++ 程序的运行时信息,这有点困难,因为 C++ 没有提供一些复杂的反射机制。现在,我的方法是使用 /clr 编译 C++ 代码,并反映 C# 生成的程序集(仅仅是因为我比 C++ 更喜欢该语言)。
虽然这一切或多或少都很好,但我现在陷入了一个困境,我需要通过调用其 main 方法来实际运行该程序。考虑到我已经走了多远,这有点令人沮丧...
这是有问题的 C++ 程序:
#include "systemc.h"
#include <iostream>
using namespace std;
// Hello_world is module name
SC_MODULE (HelloWorld) {
SC_CTOR (HelloWorld) {
// Nothing in constructor
}
void HelloWorld::say_hello() {
//Print "Hello World" to the console.
cout << "Hello World.\n";
}
};
//sc_main in top level function like in C++ main
int sc_main(int argc, char* argv[]) {
HelloWorld hello("HELLO");
hello.say_hello();
string input = "";
getline(cin, input);
return(0);
}
真的没什么花哨...
这是用于检查生成的程序集的 C# 方法:
System.Reflection.Assembly ass = System.Reflection.Assembly.LoadFrom(filename);
System.Console.WriteLine(filename + " is an assembly and has been properly loaded.");
Type[] hello = ass.GetTypes().Where(type => type.Name.ToLower().Contains("hello")).ToArray();
Type[] main = ass.GetTypes().Where(type => type.Name.ToLower().Contains("main")).ToArray();
现在,虽然 hello 类型数组包含HelloWorld 类(或者至少我假设它是那个类),main var 包含三种类型,所有这些类型都处理 doMAIN(即与我正在寻找的 sc_main 方法无关)。我认为这与它不公开有关,但将其声明为 HelloWorld 类的静态公共成员函数也不起作用,因为该函数预计是要找到的非成员函数。或者我只是忽略了一些非常愚蠢的事情?
I need to gain some run-time information about a C++ program, which is kinda difficult due to C++ not offering some sophisticated reflection mechanism. Now, my approach is to compile the C++ code using /clr and to reflect over the resulting assembly from C# (simply because I like that language more than C++).
While this is all turning out more or less fine, I'm now stuck at a point where I need to actually run the program by calling its main method. Which is kind of frustrating considering how far I got already...
This is the C++ program in question:
#include "systemc.h"
#include <iostream>
using namespace std;
// Hello_world is module name
SC_MODULE (HelloWorld) {
SC_CTOR (HelloWorld) {
// Nothing in constructor
}
void HelloWorld::say_hello() {
//Print "Hello World" to the console.
cout << "Hello World.\n";
}
};
//sc_main in top level function like in C++ main
int sc_main(int argc, char* argv[]) {
HelloWorld hello("HELLO");
hello.say_hello();
string input = "";
getline(cin, input);
return(0);
}
Nothing fancy, really...
This is the C# method used to inspect the resulting assembly:
System.Reflection.Assembly ass = System.Reflection.Assembly.LoadFrom(filename);
System.Console.WriteLine(filename + " is an assembly and has been properly loaded.");
Type[] hello = ass.GetTypes().Where(type => type.Name.ToLower().Contains("hello")).ToArray();
Type[] main = ass.GetTypes().Where(type => type.Name.ToLower().Contains("main")).ToArray();
Now, while the hello Type-array contains the HelloWorld class (or at least I assume that it is that class), the main var contains three types, all of which deal with doMAINs (ie have nothing to do with the sc_main method I'm looking for). I assume that it has something to do with it not being public, but declaring it a static public member function of the HelloWorld class doesn't work either since the function is expected to be a non-member function to be found. Or am I just overlooking something terribly stupid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,事实并非如此。您需要了解 C++/CLI 的工作原理 - 您不能仅使用 /CLR 重新编译 C++ 程序并完成它。这里的
sc_main
方法是原生的,不是托管的,你无法对其进行反射,HelloWorld 类型也是如此,除非你将它重新定义为ref 类,但我怀疑你这样做了,因为你主要按值实例化它,这只有在它是本机类时才是合法的。
.NET 和本机代码具有根本不同的语义,并且神奇的编译器开关在这方面不会为您提供帮助。
No, it doesn't. You need to learn how C++/CLI works- you can't just recompile a C++ program with /CLR and be done with it. The
sc_main
method here is native, not managed, and you can't reflect over it, and the same is true about the HelloWorld type, unless you redefined it to be aref class
, but I doubt you did because there you go in main instantiating it by value, which would only be legal if it was a native class..NET and native code have very fundamentally different semantics and a magic compiler switch will not help you in this regard.