3 Answers
3

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

Ya thanks @dennis but with out using cecil with reflection can we retrive the code or namespace is it possible
–
GowthamanSSAug 9 '12 at 6:21

@dennis u said it is possible through mono can i add it to my reference and scan the dll to list out methods
–
GowthamanSSAug 9 '12 at 6: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.
–
DennisAug 9 '12 at 6:34

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.

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.