简体   繁体   English

C#运行时的反射

[英]C# Reflection at run-time

I want to get information about the fields, functions and threads of a some project, through another project. 我想通过另一个项目获得有关某个项目的字段,函数和线程的信息。

As an example project that I want to explore, I created a field int x that initialize to 1. The Main calls to function that changes the value of x to 2. I stuck the Main in an endless loop. 作为我想要探索的示例项目,我创建了一个初始化为1的字段int .Main调用函数将x的值更改为2.我将Main卡在无限循环中。 I ran this project. 我跑了这个项目。

The second project: Get the path to the EXE file of the first project, and load the assembly. 第二个项目:获取第一个项目的EXE文件的路径,并加载程序集。 By reflection, i got the names of departments, functions and fields. 通过反思,我得到了部门,职能和领域的名称。 I tried to print the value of the field x by the function getValue. 我试图通过函数getValue打印字段x的值。 Instead of printing 2, 1 was printed. 而不是打印2,1,打印出来。 ie, the reflection refers to the values of the compiled-time, instead of the run-time. 即,反射是指编译时的值,而不是运行时。

what should i do? 我该怎么办?

You can't get the new value, that's in memory, in the other process. 在另一个过程中,您无法获得内存中的新值。

With Reflection you can however load that assembly, create an instance of that class and call Main explicitly and then check for the value of the int because then the code would be run by your process. 使用Reflection,您可以加载该程序集,创建该类的实例并显式调用Main,然后检查int的值,因为代码将由您的进程运行。

Do not expect to be able to sniff the values out of the memory space of another application with Reflection, that is not what Reflection does. 不要期望能够使用Reflection从另一个应用程序的内存空间中嗅探值,这不是Reflection所做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM