Suppose you’re developing a Visual Studio Extension and need to enumerate the references of a Visual Studio project. Here is how to do that:
public static IEnumerable<AssemblyName> CollectSettings(EnvDTE.Project project) { var vsproject = project.Object as VSLangProj.VSProject; // note: you could also try casting to VsWebSite.VSWebSite foreach (VSLangProj.Reference reference in vsproject.References) { if (reference.SourceProject == null) { // This is an assembly reference var fullName = GetFullName(reference); var assemblyName = new AssemblyName(fullName); yield return assemblyName; } else { // This is a project reference } } } public static string GetFullName(VSLangProj.Reference reference) { return string.Format("{0}, Version={1}.{2}.{3}.{4}, Culture={5}, PublicKeyToken={6}", reference.Name, reference.MajorVersion, reference.MinorVersion, reference.BuildNumber, reference.RevisionNumber, reference.Culture.Or("neutral"), reference.PublicKeyToken.Or("null")); }
For the types in the namespace VSLangProj, you’ll need to reference VSLangProj.dll. EnvDTE.Project ist located in EnvDTE.dll. And just for completness, here’s the extension method used in the code above:
static class Extensions { public static string Or(this string text, string alternative) { return string.IsNullOrWhiteSpace(text) ? alternative : text; } }