Skip to content

Commit

Permalink
Do not create false type references when calling ModuleDefinition.Get…
Browse files Browse the repository at this point in the history
…Type
  • Loading branch information
jbevain committed Mar 7, 2018
1 parent f9ac406 commit 022b6f6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Mono.Cecil/ModuleDefinition.cs
Expand Up @@ -669,7 +669,7 @@ public IEnumerable<CustomAttribute> GetCustomAttributes ()
public TypeReference GetType (string fullName, bool runtimeName)
{
return runtimeName
? TypeParser.ParseType (this, fullName)
? TypeParser.ParseType (this, fullName, typeDefinitionOnly: true)
: GetType (fullName);
}

Expand Down
10 changes: 5 additions & 5 deletions Mono.Cecil/TypeParser.cs
Expand Up @@ -234,19 +234,19 @@ string ParseAssemblyName ()
return fullname.Substring (start, position - start);
}

public static TypeReference ParseType (ModuleDefinition module, string fullname)
public static TypeReference ParseType (ModuleDefinition module, string fullname, bool typeDefinitionOnly = false)
{
if (string.IsNullOrEmpty (fullname))
return null;

var parser = new TypeParser (fullname);
return GetTypeReference (module, parser.ParseType (true));
return GetTypeReference (module, parser.ParseType (true), typeDefinitionOnly);
}

static TypeReference GetTypeReference (ModuleDefinition module, Type type_info)
static TypeReference GetTypeReference (ModuleDefinition module, Type type_info, bool type_def_only)
{
TypeReference type;
if (!TryGetDefinition (module, type_info, out type))
if (!TryGetDefinition (module, type_info, out type) && !type_def_only)
type = CreateReference (type_info, module, GetMetadataScope (module, type_info));

return CreateSpecs (type, type_info);
Expand Down Expand Up @@ -296,7 +296,7 @@ static TypeReference TryCreateGenericInstanceType (TypeReference type, Type type
var instance_arguments = instance.GenericArguments;

for (int i = 0; i < generic_arguments.Length; i++)
instance_arguments.Add (GetTypeReference (type.Module, generic_arguments [i]));
instance_arguments.Add (GetTypeReference (type.Module, generic_arguments [i], false));

return instance;
}
Expand Down
9 changes: 9 additions & 0 deletions Test/Mono.Cecil.Tests/ModuleTests.cs
Expand Up @@ -254,6 +254,15 @@ public void GetTypeNamespacePlusName ()
}
}

[Test]
public void GetNonExistentTypeRuntimeName ()
{
using (var module = GetResourceModule ("libhello.dll")) {
var type = module.GetType ("DoesNotExist", runtimeName: true);
Assert.IsNull (type);
}
}

[Test]
public void OpenModuleImmediate ()
{
Expand Down

0 comments on commit 022b6f6

Please sign in to comment.