views:

65

answers:

2

I have a relatively large solution that I compile to DLL. I would like to print out or list all the fields in every class in this project.

I'm looking for a Visual Studio feature, Visual Studio add-on, external tool, script, or code snippet (something involving reflection, perhaps?) that will allow me to simply print out all these fields.

Any ideas?

Edit:

Thanks to all the posters. Yuriy's answer was the most helpful, even though I had to massage the code a little bit. It's worth noting that I had to use the BindingFlags enum to make sure I got private fields; using the default gave public or const fields only.

FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
+5  A: 
foreach (var fieldInfo in Assembly.LoadFile(yourAssemblyDll).GetTypes()
    .SelectMany(t => t.GetFields()))
{
    Console.WriteLine(fieldInfo.Name);
}

Edit: As requested, Exception text:

System.Reflection.ReflectionTypeLoadException was unhandled
  Message="Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."
  Source="mscorlib"
  StackTrace:
       at System.Reflection.Module._GetTypesInternal(StackCrawlMark& stackMark)
       at System.Reflection.Assembly.GetTypes()
       at ConsoleApplication1.Program.Main(String[] args) in C:\Documents and Settings\Commander Math\My Documents\Visual Studio 2008\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 26
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
Yuriy Faktorovich
That gets the fields of just *one* assembly. The question explicitly asks for all the classes in the solution; what if there are multiple projects that compile to multiple assemblies?
CesarGon
@CesarGon: I thought about that too, but he specifically says "I compile to DLL", I assumed that meant one dll.
Yuriy Faktorovich
@ CesarGon - Actually, I meant *project* instead of *solution*. I'll edit my question. Good point, though!
Charlie Salts
Oh well. I should remove my downvote then, but it's too late; sorry.
CesarGon
Does this get only public fields or also protected, private and internal ones too?
jasonh
@jasonh public, to get all the private ones you have to pass in BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic to GetFields.
Yuriy Faktorovich
I've tried this code, and it works for one of my DLLs but for the DLL I really want to analyze, I get a `System.Reflection.ReflectionTypeLoadException` exception. I need to investigate.
Charlie Salts
I put this code *within* my DLL, and it works fine. Analyzing the DLL externally does not work for me - I suspect it's because I'm doing something unusual, code-wise.
Charlie Salts
Can you check the permissions, or post the full text of the ReflectionTypeLoadException?
Yuriy Faktorovich
A: 

Here is an example for getting clases. You probably want to get fields too, I assume.

http://msdn.microsoft.com/en-us/library/6ztex2dc.aspx

ram