?
Solved

How do I find all the variables declared within a particular namespace?

Posted on 2009-02-13
9
Medium Priority
?
199 Views
Last Modified: 2012-05-06
I want to be able to search my project for all variables declared as instances of classes within a particular namespace. Is there a simple way to do this?

Are there any tools to convert every variable declaration to a fully qualified declaration for example?
0
Comment
Question by:purplesoup
  • 4
  • 4
9 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 23631673
using reflection..

This example code comes from another expert: BondinASP

http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_2.0/Q_23808981.html#a22701188
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
 
LVL 5

Expert Comment

by:Imperdonato
ID: 23631732
If it's during development you want to find this out, go to Visual Studio/ View/ Object . From the window tab that opens, expand the namespace/ class you want to find this out for.

Otherwise, you'll have to use reflection, if it's for some other purpose.
0
 

Author Comment

by:purplesoup
ID: 23632019
I've tried running and code, pointing it at my assembly, but every time it tries to access the type information I get an error.

This line:
foreach (System.Type oneType in useAssembly.GetExportedTypes())

gives this error:
Could not load file or assembly '(assemblyname).(classname), Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

similarly this line:
foreach (System.Type oneType in oneModule.GetTypes())

gives this error:
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 53

Expert Comment

by:Dhaest
ID: 23632058
Dim aType As Type = GetType([MyClass])
For Each fi As System.Reflection.FieldInfo In aType.GetFields()
Debug.WriteLine(fi.Name, "field")
Next
For Each pi As System.Reflection.PropertyInfo In
aType.GetProperties()
Debug.WriteLine(pi.Name, "property")
Next
For Each mi As System.Reflection.MethodInfo In aType.GetMethods()
Debug.WriteLine(mi.Name, "method")
Next
0
 

Author Comment

by:purplesoup
ID: 23632061
Imperdonato's suggestion doesn't give me what I am looking for, so let me expand a little more.

Say for example that I have three namespaces referenced at the top of my source file:

using one.two.three;
using four.five.six;
using seven.eight.nine;

Now suppose that later in my source file I have three variables declared:

OneOne oneOne = new OneOne();
FourFour fourFour = new FourFour();
SevenSeven sevenSeven = new SevenSeven();

Which variable belongs to the one.two.three namespace?

It is difficult to answer without going to each variable declaration waving your mouse over the declaration. I have a lot of source code so it isn't practical to do this.

So what I want to be able to do is automatically change all the declarations to become fully qualified:

one.two.three.OneOne oneOne = new one.two.three.OneOne();
four.five.six.FourFour fourFour = new four.five.six.FourFour();
seven.eight.nine.SevenSeven sevenSeven = new seven.eight.nine.SevenSeven();

Now I can just search through the code and look for all the occurances of one.two.three.

It may be that reflection will also do this for me, but currently the code supplied does not work.

0
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 23632110
The easies way to solve this is to remove the using-part at the top of your page ...
You can then use the error correction options to set it to fully qualified name
Resolve.JPG
0
 

Author Comment

by:purplesoup
ID: 23632180
I've got the reflection code working - you have to put the assembly in the bin folder of the reflection visual studio project, however it doesn't give me what I want. It just lists the methods of each class, it doesn't list the fully qualified variables.
0
 

Author Comment

by:purplesoup
ID: 23632197
Dhaest's suggestion will work for a small number of files as - as far as I can tell - it requires going to each variable and right clicking, then selecting resolve.

However I have a large number of files, so I would rather not have to go through each file and do this.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 23632228
I'm not sure if there is another solution ....
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

839 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