private IProjectContent CreateCSharpProjectContent(string fileName) { IProjectContent pc = new CSharpProjectContent(); pc = pc.SetAssemblyName(AssemblyName); pc = pc.SetProjectFileName(fileName); pc = pc.SetCompilerSettings(CompilerSettings); return(pc); }
public CSharpProject(Solution solution, string title, string fileName) { // Normalize the file name fileName = Path.GetFullPath(fileName); this.Solution = solution; this.Title = title; this.FileName = fileName; // Use MSBuild to open the .csproj var msbuildProject = new Microsoft.Build.Evaluation.Project(fileName); // Figure out some compiler settings this.AssemblyName = msbuildProject.GetPropertyValue("AssemblyName"); this.CompilerSettings.AllowUnsafeBlocks = GetBoolProperty(msbuildProject, "AllowUnsafeBlocks") ?? false; this.CompilerSettings.CheckForOverflow = GetBoolProperty(msbuildProject, "CheckForOverflowUnderflow") ?? false; string defineConstants = msbuildProject.GetPropertyValue("DefineConstants"); foreach (string symbol in defineConstants.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { this.CompilerSettings.ConditionalSymbols.Add(symbol.Trim()); } // Initialize the unresolved type system IProjectContent pc = new CSharpProjectContent(); pc = pc.SetAssemblyName(this.AssemblyName); pc = pc.SetProjectFileName(fileName); pc = pc.SetCompilerSettings(this.CompilerSettings); // Parse the C# code files foreach (var item in msbuildProject.GetItems("Compile")) { var file = new CSharpFile(this, Path.Combine(msbuildProject.DirectoryPath, item.EvaluatedInclude)); Files.Add(file); } // Add parsed files to the type system pc = pc.AddOrUpdateFiles(Files.Select(f => f.UnresolvedTypeSystemForFile)); // Add referenced assemblies: foreach (string assemblyFile in ResolveAssemblyReferences(msbuildProject)) { IUnresolvedAssembly assembly = solution.LoadAssembly(assemblyFile); pc = pc.AddAssemblyReferences(new [] { assembly }); } // Add project references: foreach (var item in msbuildProject.GetItems("ProjectReference")) { string referencedFileName = Path.Combine(msbuildProject.DirectoryPath, item.EvaluatedInclude); // Normalize the path; this is required to match the name with the referenced project's file name referencedFileName = Path.GetFullPath(referencedFileName); pc = pc.AddAssemblyReferences(new[] { new ProjectReference(referencedFileName) }); } this.ProjectContent = pc; }
// Token: 0x06000021 RID: 33 RVA: 0x0000291C File Offset: 0x00000B1C private IProjectContent GetCSharpProjectContent(VSProject vsproject) { IProjectContent projectContent = new CSharpProjectContent(); Properties properties = vsproject.Project.ConfigurationManager.ActiveConfiguration.Properties; string fullName = vsproject.Project.FullName; projectContent = projectContent.SetAssemblyName(vsproject.Project.Properties.Item("AssemblyName").Value.ToString()); projectContent = projectContent.SetProjectFileName(Path.GetFileName(fullName)); projectContent = projectContent.SetLocation(fullName); return(projectContent.SetCompilerSettings(this.GetCompilerSettingsFromVSProject(vsproject))); }
public virtual void Parse() { if (References.Where(t => t.IndexOf("mscorlib.dll", StringComparison.InvariantCultureIgnoreCase) >= 0).FirstOrDefault() == null) //Don't add mscorlib twice { string mscorlib = null; if (!String.IsNullOrEmpty(TargetFrameworkVersion)) { if (TargetFrameworkVersion == "v2.0" || TargetFrameworkVersion == "v3.0" || TargetFrameworkVersion == "v3.5") { mscorlib = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll"); } } if (mscorlib == null) { mscorlib = typeof(string).Assembly.Location; } References.Add(mscorlib); } NFiles = new List <NFile>(); CSharpParser = new CSharpParser(); if (Defines != null) { Defines.ForEach(t => CSharpParser.CompilerSettings.ConditionalSymbols.Add(t)); } NFiles = SourceFiles.Select(t => new NFile { Filename = t, Project = this }).ToList(); AssemblyReferences = References.Select(t => new NAssembly { Filename = t }).ToList(); //danel: parser.ErrorPrinter ParseCsFiles(); LoadAssemblies(); var ms = StopwatchHelper.TimeInMs(LoadAssemblies); FormatLine("{0} References End: {1}ms", AssemblyReferences.Count, ms); ProjectContent = new CSharpProjectContent(); ProjectContent = (CSharpProjectContent)ProjectContent.SetAssemblyName(AssemblyName); ProjectContent = (CSharpProjectContent)ProjectContent.AddAssemblyReferences(AssemblyReferences.Select(t => t.UnresolvedAssembly).Where(t => t != null)); ProjectContent = (CSharpProjectContent)ProjectContent.AddOrUpdateFiles(NFiles.Select(t => t.UnresolvedFile)); ProjectContent.Tag = this; ms = StopwatchHelper.TimeInMs(() => Compilation = ProjectContent.CreateCompilation()); Compilation.CacheManager.SetShared(typeof(NProject), this); FormatLine("CreateCompilation {0}ms", ms); //Navigator = CreateNavigator(null); //ApplyNavigator will happen BeforeExport only on needed files, in parallel }
public CSharpProject(Solution solution, string title, string fileName) { // Normalize the file name fileName = Path.GetFullPath(fileName); Solution = solution; Title = title; FileName = fileName; // Use MSBuild to open the .csproj Project msbuildProject = new Project(fileName, null, "14.0"); // Figure out some compiler settings AssemblyName = msbuildProject.GetPropertyValue("AssemblyName"); CompilerSettings.AllowUnsafeBlocks = GetBoolProperty(msbuildProject, "AllowUnsafeBlocks") ?? false; CompilerSettings.CheckForOverflow = GetBoolProperty(msbuildProject, "CheckForOverflowUnderflow") ?? false; string defineConstants = msbuildProject.GetPropertyValue("DefineConstants"); foreach (string symbol in defineConstants.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { CompilerSettings.ConditionalSymbols.Add(symbol.Trim()); } // Initialize the unresolved type system IProjectContent pc = new CSharpProjectContent(); pc = pc.SetAssemblyName(AssemblyName); pc = pc.SetProjectFileName(fileName); pc = pc.SetCompilerSettings(CompilerSettings); // Parse the C# code files foreach (CSharpFile file in msbuildProject.GetItems("Compile").Select(item => new CSharpFile(this, Path.Combine(msbuildProject.DirectoryPath, item.EvaluatedInclude)))) { Files.Add(file); } // Add parsed files to the type system pc = pc.AddOrUpdateFiles(Files.Select(f => f.UnresolvedTypeSystemForFile)); // Add referenced assemblies: pc = ResolveAssemblyReferences(msbuildProject).Select(solution.LoadAssembly).Aggregate(pc, (current, assembly) => current.AddAssemblyReferences(assembly)); // Add project references: pc = msbuildProject.GetItems("ProjectReference").Select(item => Path.Combine(msbuildProject.DirectoryPath, item.EvaluatedInclude)).Select(Path.GetFullPath).Aggregate(pc, (current, referencedFileName) => current.AddAssemblyReferences(new ProjectReference(referencedFileName))); ProjectContent = pc; }
public void UpdateMethodsAndClassesFromCodeFile(string filename) { IProjectContent project = new CSharpProjectContent(); project.SetAssemblyName(filename); project = AddFileToProject(project, filename); var classes = new List <string>(); var typeDefinitions = project.TopLevelTypeDefinitions; foreach (var typeDef in typeDefinitions) { classes.Add(typeDef.ReflectionName); if (typeDef.Kind == TypeKind.Class) { var methods = typeDef.Methods; _queries.UpdateMethods(typeDef, methods, filename); } } }
public CodeMethodInfo UpdateMethodLocation(Method codeMethod, string fileName) { var modifiedMethodName = string.Empty; var rawMethodName = codeMethod.Name; modifiedMethodName = ConvertTrackedMethodFormatToUnitTestFormat(rawMethodName); if (codeMethod.IsConstructor) { modifiedMethodName = modifiedMethodName.Replace(".cctor", ".ctor"); } var parameters = new List <string>(); if (!modifiedMethodName.EndsWith("()")) { parameters = ParseArguments(modifiedMethodName); } modifiedMethodName = modifiedMethodName.Substring(0, modifiedMethodName.LastIndexOf('(')); IProjectContent project = new CSharpProjectContent(); project.SetAssemblyName(fileName); project = AddFileToProject(project, fileName); var classes = new List <string>(); var typeDefinitions = project.TopLevelTypeDefinitions; foreach (var typeDef in typeDefinitions) { classes.Add(typeDef.ReflectionName); if (typeDef.Kind == TypeKind.Class) { var methods = typeDef.Methods.Where(x => x.ReflectionName == modifiedMethodName); if (methods.ToList().Count > 1) { foreach (var method in methods) { System.Diagnostics.Debug.WriteLine("Method : " + method.Name); if (method.Parameters.Count == parameters.Count) { bool parametersMatch = true; for (int i = 0; i < method.Parameters.Count; i++) { var methodParameterType = method.Parameters[i].Type; string typeName = methodParameterType.ToString(); typeName = typeName.Replace("bool", "Boolean") .Replace("System.Nullable[[Boolean]]", "System.Nullable[[bool]]") .Replace("System.Guid", "Guid"); if (typeName.Equals(parameters[i], StringComparison.OrdinalIgnoreCase) && parametersMatch) { parametersMatch = true; } else { parametersMatch = false; break; } } if (parametersMatch) { return(new CodeMethodInfo { RawMethodName = rawMethodName, FileName = fileName, Line = method.BodyRegion.BeginLine, Column = method.BodyRegion.BeginColumn }); } } } } else if (methods.Count() == 1) { return(new CodeMethodInfo { RawMethodName = rawMethodName, FileName = fileName, Line = methods.First().BodyRegion.BeginLine, Column = methods.First().BodyRegion.BeginColumn }); } } } return(null); }