static Context.ProjectOutputType GetProjectOutputType(Context.ProjectType sourceProjectType, Func <string, string> functionToGetAProjectProperty, Func <string, string> functionToGetACSProjProperty) { Context.ProjectOutputType projectOutputType = Context.ProjectOutputType.Unknown; // Default value if (sourceProjectType == Context.ProjectType.WPF) { string outputTypeProperty = functionToGetAProjectProperty("OutputType"); if (outputTypeProperty == "1") { projectOutputType = Context.ProjectOutputType.Application; } else if (outputTypeProperty == "2") { projectOutputType = Context.ProjectOutputType.Application; } } else if (sourceProjectType == Context.ProjectType.Silverlight) { var silverlightApplicationPropertyValue = functionToGetACSProjProperty("SilverlightApplication"); if (silverlightApplicationPropertyValue != null) { if (silverlightApplicationPropertyValue.Trim().ToLower() == "true") { projectOutputType = Context.ProjectOutputType.Application; } else if (silverlightApplicationPropertyValue.Trim().ToLower() == "false") { projectOutputType = Context.ProjectOutputType.Library; } } } return(projectOutputType); }
static Context.ProjectType GetProjectType(string projectTypeGuids) { Context.ProjectType projectType = Context.ProjectType.Other; // Default value if (projectTypeGuids.Contains(SilverlightProjectTypeGuid)) { projectType = Context.ProjectType.Silverlight; } else if (projectTypeGuids.Contains(WpfProjectTypeGuid)) { projectType = Context.ProjectType.WPF; } return(projectType); }
static bool TryLoadProjectFromCsproj(string csprojFullPath, out Microsoft.Build.Evaluation.Project project, out string projectName, out Context.ProjectType projectType, out Context.ProjectOutputType projectOutputType) { try { Microsoft.Build.Evaluation.ProjectCollection collection = new Microsoft.Build.Evaluation.ProjectCollection(); collection.DefaultToolsVersion = "4.0"; Microsoft.Build.Evaluation.Project theProject = collection.LoadProject(csprojFullPath); string projectTypeGuids = theProject.GetPropertyValue("ProjectTypeGuids"); //cf. https://github.com/Microsoft/visualfsharp/blob/master/vsintegration/src/FSharp.ProjectSystem.Base/Project/ProjectFactory.cs string isSilverlightApplicationString = theProject.GetPropertyValue("SilverlightApplication"); // Get the project name: projectName = System.IO.Path.GetFileNameWithoutExtension(csprojFullPath); // Get the Project Type: projectType = GetProjectType(projectTypeGuids); // Get the project Output Type: Func <string, string> functionToGetAProjectProperty = (string propertyName) => { return(theProject.GetPropertyValue(propertyName)); }; projectOutputType = GetProjectOutputType(projectType, functionToGetAProjectProperty, functionToGetAProjectProperty); // Return the project itself as well: project = theProject; return(true); } catch (Exception ex) { MessageBox.Show(ex.ToString()); project = null; projectName = null; projectType = Context.ProjectType.Other; projectOutputType = Context.ProjectOutputType.Unknown; return(false); } }
static void GetProjectInformation(EnvDTE.Project project, EnvDTE.Solution solution, out string fullPathOfCSProj, out Context.ProjectType projectType, out Context.ProjectOutputType projectOutputType) { string projectTypeGuids = GetProjectTypeGuids(project); // Get the full path of the CSPROJ: fullPathOfCSProj = project.FullName; // Get the Project Type: projectType = GetProjectType(projectTypeGuids); // Define some helpful functions: Func <string, string> functionToGetAProjectProperty = (string propertyName) => { var property = project.Properties.Item(propertyName); // Credits: https://www.mztools.com/articles/2007/mz2007014.aspx if (property != null && property.Value != null) { return(property.Value.ToString()); } else { return(null); } }; Func <string, string> functionToGetACSProjProperty = (string propertyName) => { return(GetPropertyFromCSProj(propertyName, "Debug", project)); }; // Get the project Output Type: projectOutputType = GetProjectOutputType(projectType, functionToGetAProjectProperty, functionToGetACSProjProperty); }