1

Possible Duplicate:
Finding all Namespaces in an assembly using Reflection (DotNET)

let us consider that i am having list of 10 to 30 dll with me i need to list out all the namespaces imported by those dll.

I have tried with following code using reflection as

string[] fileEntries = Directory.GetFiles(sDir, "*.dll");
                foreach (string fileName in fileEntries)
                {
                    Assembly assembly = Assembly.LoadFrom(fileName);
                    List<string> namespaces = new List<string>();
                    foreach (var type in assembly.GetTypes())
                    {
                        string ns = type.Namespace;
                        if (!namespaces.Contains(ns))
                        {
                            namespaces.Add(ns);
                            Console.WriteLine(ns);
                        }
                    }

                }

but it only retrives the only the namespace of those dll.

for example let us consider a program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace sample
{
    public partial class sample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string pth = filepth("D:/sample1/s.txt");
            createfile(pth);
            Response.Write("File created on path :" + pth);
        }
     }      
}

based on the above code it retrives only sample but i need to retrive all namespaces used as

**

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;

**

can you guide me regarding it..

waiting for your valuable comments

Community
  • 1
  • 1
GowthamanSS
  • 1,434
  • 4
  • 33
  • 58

3 Answers3

2

There's no any namespaces at run-time. Moreover, in the sample you provided, not every namespace from source code used (execute "Organize usings -> Remove unused usings" from the context menu).

You can look up referenced assemblies for particular assembly, not a namespaces. To do what you want, you need to know every type, used (not declared, which is easy to get!) in particular assembly's code.

I don't know, how it can be done without analyzing IL code. Hope, Cecil can help you... but it's not easy and not reflection-only task.

Citation from the remarks for MethodBody class in MSDN:

You can use the token-resolution methods of the module class, such as ResolveType, ResolveMethod, and ResolveType, to resolve the tokens in the method body to Type objects, MethodInfo objects, and FieldInfo objects that provide detailed information about the types, methods, and fields accessed by the MSIL in the method body. Parsing method bodies requires a thorough understanding of metadata and MSIL instruction formats. Information can be found in the Common Language Infrastructure (CLI) documentation

Do you want to learn CLI specification yourself? :)

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • There's no namespaces, but there are fully qualified type names :) – Simon Whitehead Aug 09 '12 at 05:41
  • Ya thanks @dennis but with out using cecil with reflection can we retrive the code or namespace is it possible – GowthamanSS Aug 09 '12 at 06:21
  • @dennis u said it is possible through mono can i add it to my reference and scan the dll to list out methods – GowthamanSS Aug 09 '12 at 06:27
  • @GowthamanSS: 1) you can get types of every field in every type in your assembly 2) you can get type of input parameters and return value for every property and method; 3) you can even get types of every local variable in every method (see `MethodBase.GetMethodBody` method and `MethodBody` class, especially, remarks section). **But**, I don't know a way how to look up for static method calls (e.g., LINQ extension methods) without analyzing IL - you **must** get corresponding metadata tokens. – Dennis Aug 09 '12 at 06:34
  • @Dennis ya thanks will check it now is it possible to find whether connection.open is used in particular dll – GowthamanSS Aug 09 '12 at 06:36
0

What you want is not an easy task and the reflection is certainly not the way to go. Mainly because it wont give you access to source of methods that refer to classes that they are using.

The only solution I can think of would be tool as cci that provides you with means to traverse through all components of dll. Especially you can gain access to every type/method referenced form every line of code and generate the list you desire.

Rafal
  • 12,391
  • 32
  • 54
0

Right click the using block and you'll find "organize usings" in the context menu, where there is a function called "remove unused usings".

The above sentense is unrelated with your question but it tells us a truth that the usings in the using block in top of a .cs file are sometimes not used. It's really very hard to tell whether a namespace is used or not, unless you analyze the whole code like Visual Studio.

And using reflection, like assembly.GetTypes() in your code, gives the types defined in the assembly, not used in the assembly.

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174