Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add importing of netmodule #819

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions Mono.Cecil/Import.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,14 +521,33 @@ protected IMetadataScope ImportScope (IMetadataScope scope)
return ImportReference ((AssemblyNameReference) scope);
case MetadataScopeType.ModuleDefinition:
if (scope == module) return scope;
return ImportReference (((ModuleDefinition) scope).Assembly.Name);
var moduleDefinition = (ModuleDefinition) scope;
if (moduleDefinition.Assembly == null)
return ImportReference (moduleDefinition);
else
return ImportReference (moduleDefinition.Assembly.Name);
case MetadataScopeType.ModuleReference:
throw new NotImplementedException ();
return ImportReference ((ModuleReference) scope);
}

throw new NotSupportedException ();
}

public virtual ModuleReference ImportReference (ModuleReference name)
{
Mixin.CheckName (name);

ModuleReference reference;
if (module.TryGetModuleReference (name, out reference))
return reference;

reference = new ModuleReference (name.Name);

module.ModuleReferences.Add (reference);

return reference;
}

public virtual AssemblyNameReference ImportReference (AssemblyNameReference name)
{
Mixin.CheckName (name);
Expand Down Expand Up @@ -771,6 +790,23 @@ public static bool TryGetAssemblyNameReference (this ModuleDefinition module, As
return false;
}

public static bool TryGetModuleReference (this ModuleDefinition module, ModuleReference name_reference, out ModuleReference module_reference)
{
var references = module.ModuleReferences;

for (int i = 0; i < references.Count; i++) {
var reference = references [i];
if (!Equals (name_reference, reference))
continue;

module_reference = reference;
return true;
}

module_reference = null;
return false;
}

static bool Equals (byte [] a, byte [] b)
{
if (ReferenceEquals (a, b))
Expand Down Expand Up @@ -808,5 +844,14 @@ static bool Equals (AssemblyNameReference a, AssemblyNameReference b)
return false;
return true;
}

static bool Equals (ModuleReference a, ModuleReference b)
{
if (ReferenceEquals (a, b))
return true;
if (a.Name != b.Name)
return false;
return true;
}
}
}
14 changes: 14 additions & 0 deletions Test/Mono.Cecil.Tests/ImportCecilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,20 @@ public void ContextGenericTest ()
Assert.AreEqual ("Mono.Cecil.Tests.ImportCecilTests/Generic`1<TS> Mono.Cecil.Tests.ImportCecilTests/Generic`1<System.String>::ComplexGenericMethod<TS>(T,TS)", method.FullName);
}

[Test]
public void ImportTypeFromOtherModule ()
{
var fooModule = ModuleDefinition.CreateModule("Foo", ModuleKind.NetModule);
var foo = new TypeDefinition("", "Foo", 0, fooModule.TypeSystem.Object);
fooModule.Types.Add(foo);

var mainModule = ModuleDefinition.CreateModule("Main", ModuleKind.NetModule);
mainModule.ModuleReferences.Add(fooModule);
var program = new TypeDefinition("", "Program", TypeAttributes.Class, mainModule.TypeSystem.Object);
var importedFooType = mainModule.ImportReference(foo);
Assert.AreEqual ("Foo", importedFooType.FullName);
}

delegate void Emitter (ModuleDefinition module, MethodBody body);

static TDelegate Compile<TDelegate> (Emitter emitter, [CallerMemberName] string testMethodName = null)
Expand Down