static dynamic GetReference(ResolveResult result, AstNode node, CSharpFile file) { if (result == null) { return null; } object valid = null; if (result is MethodGroupResolveResult) { valid = ((MethodGroupResolveResult)result).Methods.FirstOrDefault( m => searchedMembers.Any(member => member is IMethod && ((IMethod)member).Region == m.Region)); } else if (result is MemberResolveResult) { var foundMember = ((MemberResolveResult)result).Member; valid = searchedMembers.FirstOrDefault( member => member is IMember && ((IMember)member).Region == foundMember.Region); } else if (result is NamespaceResolveResult) { var ns = ((NamespaceResolveResult)result).NamespaceName; valid = searchedMembers.FirstOrDefault(n => n is string && n.ToString() == ns); } else if (result is LocalResolveResult) { var ns = ((LocalResolveResult)result).Variable; valid = searchedMembers.FirstOrDefault(n => n is IVariable && ((IVariable)n).Region == ns.Region); } else if (result is TypeResolveResult) { valid = searchedMembers.FirstOrDefault(n => n is IType); } else { valid = searchedMembers.FirstOrDefault(); } if (node is ConstructorInitializer) return null; if (node is ObjectCreateExpression) node = ((ObjectCreateExpression)node).Type; if (node is InvocationExpression) node = ((InvocationExpression)node).Target; if (node is MemberReferenceExpression) node = ((MemberReferenceExpression)node).MemberNameToken; if (node is MemberType) node = ((MemberType)node).MemberNameToken; if (node is TypeDeclaration && (searchedMembers.First() is IType)) node = ((TypeDeclaration)node).NameToken; if (node is DelegateDeclaration) node = ((DelegateDeclaration)node).NameToken; if (node is EntityDeclaration && (searchedMembers.First() is IMember)) node = ((EntityDeclaration)node).NameToken; if (node is ParameterDeclaration && (searchedMembers.First() is IParameter)) node = ((ParameterDeclaration)node).NameToken; if (node is ConstructorDeclaration) node = ((ConstructorDeclaration)node).NameToken; if (node is DestructorDeclaration) node = ((DestructorDeclaration)node).NameToken; if (node is NamedArgumentExpression) node = ((NamedArgumentExpression)node).NameToken; if (node is NamedExpression) node = ((NamedExpression)node).NameToken; if (node is VariableInitializer) node = ((VariableInitializer)node).NameToken; if (node is IdentifierExpression) { node = ((IdentifierExpression)node).IdentifierToken; } var region = new DomRegion(file.FileName, node.StartLocation, node.EndLocation); var length = node is PrimitiveType ? keywordName.Length : node.EndLocation.Column - node.StartLocation.Column; return new { valid = valid, Region = region, Length = length, File = file }; }
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; }