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
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
MethodBodyclass in MSDN:Do you want to learn CLI specification yourself? 🙂