Java变量在内存,用java在内存中显示变量

本文介绍了如何在C#和Java中通过反射获取程序内存中的所有静态变量。对于C#,可以使用System.Reflection相关API来获取程序集、类型以及静态字段。对于Java的解决方案,虽然没有直接给出,但建议根据C#的方法思路进行类似实现,因为两者反射设计相似。
摘要由CSDN通过智能技术生成

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

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值