通过Java显示内存中的变量 [英] show variable in memory by java

查看:220
本文介绍了通过Java显示内存中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
无法获取内存中程序的所有静态变量
或通过Java中的c#在内存中将静态变量引用给应用程序
感谢您的帮助

hi every one
there is no way to get all static variable for program in memory
or ref of static variable to application by c# in memory by java
thanks for any help

推荐答案

使用.NET,我怀疑使用Java也很容易.

让我回答有关.NET的知识,我对此了解得多.所有静态变量都在类或结构中声明,而不是在方法内部声明.

考虑一下您对内存感兴趣的程序集(使用System.Reflection.Assembly.GetAssemblySystem.Reflection.Assembly.GetCallingAssemblySystem.Reflection.Assembly.GetEntryAssemblySystem.Reflection.Assembly.GetExecutingAssembly获得的程序集).对于感兴趣的程序集,请使用System.Assembly.GetType获取所有声明的类型.采用所有类型,包括私有和非私有.对于每种类型(跳过枚举文件),请使用System.Type.GetFields和参数bindingFlags并使用值System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic获取所有静态字段(这些标志是最重要的详细信息).

现在,您有了程序集中所有静态字段成员的列表.此列表的元素的类型为System.Reflection.FieldInfo.不要包括具有以下谓词属性IsInitOnly == true的成员(它们是只读的).您无需检查谓词IsStatic,因为bindingFlags已将非静态谓词过滤掉了(请参见上文).问题已解决.

您可以使用这些思想在Java中开发类似的算法-Java和C#反射设计或多或少相似.

—SA
With .NET, this is easy, I suspect, with Java too.

Let me answer about .NET, which I know much better. All static variable are declared in classes or structures and not inside methods.

Consider you have the assembly of interest in memory (the one you obtain using System.Reflection.Assembly.GetAssembly, System.Reflection.Assembly.GetCallingAssembly, System.Reflection.Assembly.GetEntryAssembly or System.Reflection.Assembly.GetExecutingAssembly). For the assembly of interest, get all declared type using System.Assembly.GetType. Take all types, including private and non-private. For each type (skip enumeration files), get all static fields using System.Type.GetFields and parameter bindingFlags with the value System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic (these flags is the most important detail).

Now you have the list of all the static field members in the assembly. The type of the element of this list is System.Reflection.FieldInfo. Do not include members with the following predicate properties IsInitOnly == true (they are read-only). You don''t need to check predicate IsStatic, as non-static are already filtered out by the bindingFlags (see above). The problem is solved.

You can use the ideas to develop similar algorithm in Java — Java and C# Reflection designs are more or less similar.

—SA


这篇关于通过Java显示内存中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆