Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 5-Mar-11 21:00pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Mar-11 3:01am    
As C# is mentioned, I added the tag .NET (language does not matter)
--SA
Sergey Alexandrovich Kryukov 6-Mar-11 3:02am    
Absolutely unclear: "application by c# in memory by java"; I provided .NET Answer.
--SA

1 solution

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
 
Share this answer
 
Comments
Richard MacCutchan 6-Mar-11 3:36am    
Nice detailed answer.
Sergey Alexandrovich Kryukov 6-Mar-11 3:38am    
Thank you.
--SA
Mostafa Elsadany 6-Mar-11 14:57pm    
thanks for help
but you don't know any idea about java
Sergey Alexandrovich Kryukov 6-Mar-11 20:08pm    
Sorry about that. Maybe someone else will answer, must be more or less similar to what I described about .NET. You actually could find out by yourself, if you know Java better.
--SA
Sergey Alexandrovich Kryukov 6-Mar-11 20:09pm    
By the way, could you explain why you need both Java and .NET, what's your idea behind that? See my comment to your original Question.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900