Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 9796b70

Browse files
committed
Fix ITypeDefinition.GetModel() for types defined in referenced assemblies; and use it in BaseTypesTreeNode.
1 parent e25c694 commit 9796b70

2 files changed

Lines changed: 27 additions & 64 deletions

File tree

src/Main/Base/Project/Dom/ClassBrowser/BaseTypesTreeNode.cs

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -59,74 +59,15 @@ void UpdateBaseTypes()
5959
if (currentTypeDef != null) {
6060
foreach (var baseType in currentTypeDef.DirectBaseTypes) {
6161
ITypeDefinition baseTypeDef = baseType.GetDefinition();
62-
if ((baseTypeDef != null)) {
63-
ITypeDefinitionModel baseTypeModel = GetTypeDefinitionModel(currentTypeDef, baseTypeDef);
62+
if (baseTypeDef != null) {
63+
ITypeDefinitionModel baseTypeModel = baseTypeDef.GetModel();
6464
if (baseTypeModel != null)
6565
baseTypes.Add(baseTypeModel);
6666
}
6767
}
6868
}
6969
}
7070

71-
ITypeDefinitionModel GetTypeDefinitionModel(ITypeDefinition mainTypeDefinition, ITypeDefinition baseTypeDefinition)
72-
{
73-
ITypeDefinitionModel resolveTypeDefModel = null;
74-
var assemblyFileName = mainTypeDefinition.ParentAssembly.GetRuntimeAssemblyLocation();
75-
IAssemblyModel assemblyModel = null;
76-
77-
try {
78-
// Try to get AssemblyModel from project list
79-
IProjectService projectService = SD.GetRequiredService<IProjectService>();
80-
if (projectService.CurrentSolution != null) {
81-
var projectOfAssembly = projectService.CurrentSolution.Projects.FirstOrDefault(p => p.AssemblyModel.Location == assemblyFileName);
82-
if (projectOfAssembly != null) {
83-
// We automatically have an AssemblyModel from project
84-
assemblyModel = projectOfAssembly.AssemblyModel;
85-
}
86-
}
87-
88-
var assemblyParserService = SD.GetService<IAssemblyParserService>();
89-
if (assemblyModel == null) {
90-
if (assemblyParserService != null) {
91-
if (assemblyFileName != null) {
92-
assemblyModel = assemblyParserService.GetAssemblyModel(assemblyFileName);
93-
}
94-
}
95-
}
96-
97-
if (assemblyModel != null) {
98-
// Nothing in projects, load from assembly file
99-
resolveTypeDefModel = assemblyModel.TopLevelTypeDefinitions[baseTypeDefinition.FullTypeName];
100-
if (resolveTypeDefModel != null) {
101-
return resolveTypeDefModel;
102-
}
103-
104-
// Look at referenced assemblies
105-
if ((assemblyModel.References != null) && (assemblyParserService != null)) {
106-
foreach (var referencedAssemblyName in assemblyModel.References.AssemblyNames) {
107-
CombinedAssemblySearcher searcher = new CombinedAssemblySearcher();
108-
if ((assemblyModel.Context != null) && (assemblyModel.Context.Project != null)) {
109-
searcher.AddSearcher(new ProjectAssemblyReferenceSearcher(assemblyModel.Context.Project));
110-
}
111-
searcher.AddSearcher(new DefaultAssemblySearcher(assemblyModel.Location));
112-
var resolvedFile = searcher.FindAssembly(referencedAssemblyName.AssemblyName);
113-
if (resolvedFile != null) {
114-
var referenceAssemblyModel = assemblyParserService.GetAssemblyModel(resolvedFile);
115-
resolveTypeDefModel = referenceAssemblyModel.TopLevelTypeDefinitions[baseTypeDefinition.FullTypeName];
116-
if (resolveTypeDefModel != null) {
117-
return resolveTypeDefModel;
118-
}
119-
}
120-
}
121-
}
122-
}
123-
} catch (Exception) {
124-
// TODO Can't load the type, what to do?
125-
}
126-
127-
return resolveTypeDefModel;
128-
}
129-
13071
protected override System.Collections.Generic.IComparer<ICSharpCode.TreeView.SharpTreeNode> NodeComparer {
13172
get {
13273
return NodeTextComparer;

src/Main/Base/Project/Util/SharpDevelopExtensions.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,28 @@ public static IEntityModel GetModel(this IEntity entity)
497497
return null;
498498
}
499499

500+
/// <summary>
501+
/// Retrieves the model instance for the given assembly.
502+
/// May return null if there is no model for the specified assembly.
503+
/// </summary>
504+
public static IAssemblyModel GetModel(this IAssembly assembly)
505+
{
506+
if (assembly == null)
507+
throw new ArgumentNullException("assembly");
508+
509+
IProject project = assembly.GetProject();
510+
if (project != null)
511+
return project.AssemblyModel;
512+
513+
try {
514+
return SD.AssemblyParserService.GetAssemblyModel(assembly.GetReferenceAssemblyLocation());
515+
} catch (Exception ex) {
516+
// TODO: use the exact exception types that GetAssemblyModel() throws (+document them)
517+
// silently ignore errors when loading the assembly
518+
return null;
519+
}
520+
}
521+
500522
/// <summary>
501523
/// Retrieves the model instance for the given type definition.
502524
/// May return null if there is no model for the specified type definition.
@@ -506,9 +528,9 @@ public static ITypeDefinitionModel GetModel(this ITypeDefinition typeDefinition)
506528
if (typeDefinition == null)
507529
throw new ArgumentNullException("typeDefinition");
508530

509-
IProject project = typeDefinition.ParentAssembly.GetProject();
510-
if (project != null)
511-
return project.AssemblyModel.TopLevelTypeDefinitions[typeDefinition.FullTypeName];
531+
IAssemblyModel assembly = typeDefinition.ParentAssembly.GetModel();
532+
if (assembly != null)
533+
return assembly.TopLevelTypeDefinitions[typeDefinition.FullTypeName];
512534
else
513535
return null;
514536
}

0 commit comments

Comments
 (0)