[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Can I retrieve a list of variable from a .Net assembly?

Posted on 2008-10-13
5
Medium Priority
?
291 Views
Last Modified: 2013-12-17
Hi,

I would like to know if there is a way how to retrieve a list of variable (all modifiers) that make up an assembly.  Basically I am developing a Software Testing automation tool as part of my studies and would need to inspect the contents of the variables that make up the assembly under test.  I have already managed to get the list of properties that make up the interfaces (GUI) of the assembly, the variables used is my next target.

I was thinking maybe the System.Reflection namespace might be helpful, not sure how though.

Hope I was clear in my explanation...feel free to ask in case I was not clear enough.

Any help is appreciated.

Thanks
0
Comment
Question by:b0rnslipy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 2000 total points
ID: 22701188
refer attached code
using System;
using System.Collections.Generic;
using System.Text;
 
using System.Security.Cryptography;
namespace ConsoleApplication
{
 
public class Tester
{
    public static void Main()
    {
        System.Reflection.Assembly useAssembly = default(System.Reflection.Assembly);
        
        try {
            useAssembly = System.Reflection.Assembly.LoadFile(@"Your assembley");
        }
        catch (System.Exception ex) {
            Console.WriteLine(ex.Message);
            return;
        }
        
        
        Console.WriteLine("Full Name: " + useAssembly.FullName);
        
        Console.WriteLine("Resources");
        foreach (string oneName in useAssembly.GetManifestResourceNames()) {
            Console.WriteLine("   - " + oneName);
        }
        
        Console.WriteLine("Exported Types");
        foreach (System.Type oneType in useAssembly.GetExportedTypes()) {
            Console.WriteLine("   - " + oneType.Name);
        }
        
        Console.WriteLine("Modules");
        foreach (System.Reflection.Module oneModule in useAssembly.GetLoadedModules()) {
            Console.WriteLine("   - " + oneModule.Name);
            foreach (System.Type oneType in oneModule.GetTypes()) {
                Console.WriteLine("     Type: " + oneType.Name);
                foreach (System.Reflection.FieldInfo oneField in oneType.GetFields()) {
                    Console.WriteLine("        Field: " + oneField.ToString());
                }
                
                foreach (System.Reflection.MethodInfo oneMethod in oneType.GetMethods()) {
                    Console.WriteLine("        Method: " + oneMethod.ToString());
                }
            }
        }
        
    }
    
}
 
}

Open in new window

0
 

Author Comment

by:b0rnslipy
ID: 22701354
Hi BondinASP,

I think we're close, but it's not exactly what I need.

The scenario is, if for example in the assembly there is a code decalring a variable such as

Public string strName;

I need some code which will return strName as part of the list (together with other variables that are declared within that assembly.  I will need this as I will later need to watch the value changes within these variables.

If the code you provided should allow for this, then I must be doing something wrong.

Looking forward for your reply.
0
 

Author Comment

by:b0rnslipy
ID: 22701536
Actually I kept on trying and with a few minor changes and it worked!

This is the key part of your code which gave me what I needed:

BindingFlags getAllFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;

foreach (System.Type oneType in oneModule.GetTypes())
{
                            foreach (System.Reflection.FieldInfo oneField in oneType.GetFields(getAllFlags))
                            {
                                dCollect.chBxFieldsToVerify.Items.Add(oneField.ToString());
                            }
}

I added the getAllFlags so that I can retrieve all modifiers, as by default it retrieves only public members.

Thanks very much for your help.
0
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22703748
your welcome
0
 

Author Comment

by:b0rnslipy
ID: 22710555
Erm, one other thing...is there any way how I can now watch the values of these retrieved variables while my application is executing this assemly?

Feel free to help on this thread http://www.experts-exchange.com/Programming/Languages/Q_23811128.html 

Thanks,

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question