You can use ITypeDiscoveryService
to list all available types in project.
To do this, you should add Microsoft.VisualStudio.Shell.Design
reference to project. This way you can use DynamicTypeService
to get an instance of ITypeDiscoveryService
.
Add this methods to your Package
class:
public List<Type> GetAllTypes()
{
var trs = GetTypeDiscoveryService();
var types = trs.GetTypes(typeof(object), true /*excludeGlobalTypes*/);
var result = new List<Type>();
foreach (Type type in types)
{
if (type.IsPublic)
{
if (!result.Contains(type))
result.Add(type);
}
}
return result;
}
private ITypeDiscoveryService GetTypeDiscoveryService()
{
var dte = GetService<EnvDTE.DTE>();
var typeService = GetService<DynamicTypeService>();
var solution = GetService<IVsSolution>();
IVsHierarchy hier;
var projects = dte.ActiveSolutionProjects as Array;
var currentProject = projects.GetValue(0) as Project;
solution.GetProjectOfUniqueName(currentProject.UniqueName, out hier);
return typeService.GetTypeDiscoveryService(hier);
}
private T GetService<T>()
{
return (T)GetService(typeof(T));
}
Then you can use GetAllTypes
to get all types of active project:
List<Type> types= GetAllTypes();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…