VS version detection

Apr 23, 2014 at 9:01 PM
I suggest this change in CodeGeneratorFactory.cs in order to detect VS version
public ICodeGenerator Create(string className, string defaultNamespace, string inputFileContents, CodeDomProvider codeDomProvider = null, TypeAttributes? classAccessibility = null)
        {
            DTE dte = Package.GetGlobalService(typeof(SDTE)) as DTE;
            VisualStudioVersion vsVersion = VisualStudioVersion.VS2012;
            if (dte != null)
            {
                Version dteVersion;
                if (Version.TryParse(dte.Version, out dteVersion) && dteVersion >= new Version(12, 0))
                    vsVersion = VisualStudioVersion.VS2013;
            }
            return new CodeDomCodeGenerator(new ResourceParser(inputFileContents), className, defaultNamespace, codeDomProvider, classAccessibility, vsVersion);
        }
Coordinator
Apr 24, 2014 at 7:47 AM
Thanks!

I just tested it and it works as expected. I'll publish an update tonight
Apr 24, 2014 at 11:43 AM
I've found a better implementation that checks if TargetPlatformVersion is 8.1 or higher (support for ResourceLoader.GetForCurrentView) so that if you target a Vs2013 to 8.0 it uses the default ResourceLoader constructor
 public ICodeGenerator Create(string className, string defaultNamespace, string inputFileContents, CodeDomProvider codeDomProvider = null, TypeAttributes? classAccessibility = null)
        {
            VisualStudioVersion vsVersion = VisualStudioVersion.VS2012;
            var project = GetActiveProject();
            if (project != null)
            {
                Version platformVersion;
                if (Version.TryParse(GetRawPropertyValue<String>(project, "TargetPlatformVersion"), out platformVersion) && platformVersion >= new Version(8, 1))
                    vsVersion = VisualStudioVersion.VS2013;
            }
            return new CodeDomCodeGenerator(new ResourceParser(inputFileContents), className, defaultNamespace, codeDomProvider, classAccessibility, vsVersion);
        }

        private static T GetRawPropertyValue<T>(Project project, String name)
        {
            try
            {
                XDocument document = XDocument.Load(project.FullName);
                if (document == null)
                    return default(T);

                XElement propertyElement = document.Descendants(XName.Get(name, "http://schemas.microsoft.com/developer/msbuild/2003")).FirstOrDefault();
                if (propertyElement == null)
                    return default(T);

                return (T)Convert.ChangeType(propertyElement.Value, typeof(T));
            }
            catch (Exception)
            {
                return default(T);
            }
        }

        private static Project GetActiveProject()
        {
            DTE dte = Package.GetGlobalService(typeof(SDTE)) as DTE;
            if (dte != null)
            {
                Array activeSolutionProjects = dte.ActiveSolutionProjects as Array;
                if (activeSolutionProjects != null && activeSolutionProjects.Length > 0)
                    return activeSolutionProjects.GetValue(0) as Project;
            }
            return null;
        }
}