我找到了解決方案。事實上,可以從代碼生成器中獲取對Visual Studio項目對象的引用。
我發現代碼及彼項目參考:http://subversion.assembla.com/svn/mistral-framework/trunk/src/VisualStudio/Mistral.VisualStudio.XmlClassGenerator/BaseCodeGeneratorWithSite.cs
下面的方法,然後返回到裝配對象,我們可以用它來獲取所有可用的類型等通過反射的參考。
private static Assembly GetAssembly(Project project, string assemblyName)
{
Microsoft.VisualStudio.OLE.Interop.IServiceProvider oleSP =
project.DTE as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
ServiceProvider sp = new ServiceProvider(oleSP);
DynamicTypeService dts =
sp.GetService(typeof(DynamicTypeService)) as DynamicTypeService;
Microsoft.VisualStudio.Shell.Interop.IVsSolution sln =
sp.GetService(typeof(SVsSolution)) as IVsSolution;
Microsoft.VisualStudio.Shell.Interop.IVsHierarchy hier = null;
sln.GetProjectOfUniqueName(project.UniqueName, out hier);
ITypeResolutionService rs = dts.GetTypeResolutionService(hier);
return rs.GetAssembly(new AssemblyName(assemblyName), true);
}
該方法仍然需要程序集名稱,這對我來說很好,因爲我可以從XML基本文件中讀取它來生成。不過,我很確定你可以得到沒有名字的參考。 (我只是懶惰,並停止調查,當我得到它的工作;-)
來源
2010-11-08 06:56:37
jfs
聽起來類似於這個http://csd.codeplex.com/其開源,也許它的代碼將幫助你 – Iain 2010-11-05 04:32:28