2009-12-12 66 views
2

我有一個相對較大的解決方案,我編譯爲DLL。我想打印出列表全部這個項目的每個班的領域。獲取項目中所有字段的列表

我正在尋找一個Visual Studio功能,Visual Studio插件,外部工具,腳本或代碼片段(涉及反射,也許?),這將允許我只需打印出所有這些字段。

任何想法?

編輯:

感謝所有的海報。尤里的回答是最有幫助的,儘管我不得不按摩一些代碼。值得注意的是,我不得不使用BindingFlags枚舉來確保我獲得了私有字段;使用默認只給公共或常量字段。

FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); 

回答

5
foreach (var fieldInfo in Assembly.LoadFile(yourAssemblyDll).GetTypes() 
    .SelectMany(t => t.GetFields())) 
{ 
    Console.WriteLine(fieldInfo.Name); 
} 

編輯:按照要求,異常文本:

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: 
+0

這只是得到一個* *裝配領域。該問題明確要求解決方案中的所有類;如果有多個項目可以編譯成多個程序集呢? – CesarGon 2009-12-12 01:11:21

+0

@CesarGon:我也想過,但他特別說「我編譯成DLL」,我認爲這意味着一個DLL。 – 2009-12-12 01:12:28

+0

@ CesarGon - 其實,我的意思是*項目*而不是*解決方案*。我會編輯我的問題。雖然好點! – 2009-12-12 01:12:41

相關問題