public void CloneHelloWorldProgramType() { var sourceModule = ModuleDefinition.FromBytes(Properties.Resources.HelloWorld); var targetModule = PrepareTempModule(); var programType = sourceModule.TopLevelTypes.First(t => t.Name == "Program"); var nestedType = new TypeDefinition("", "Nested", PE.DotNet.Metadata.Tables.Rows.TypeAttributes.NestedPublic); programType.NestedTypes.Add(nestedType); var notNestedType = new TypeDefinition("", "NotNested", PE.DotNet.Metadata.Tables.Rows.TypeAttributes.Public); sourceModule.TopLevelTypes.Add(notNestedType); var result = new MemberCloner(targetModule) .Include(programType, notNestedType) .Clone(); Assert.Contains(nestedType, result.OriginalMembers); Assert.Contains(result.GetClonedMember(nestedType), result.ClonedMembers); Assert.DoesNotContain(result.GetClonedMember(nestedType), result.ClonedTopLevelTypes); Assert.Contains(result.GetClonedMember(notNestedType), result.ClonedTopLevelTypes); Assert.Contains(result.GetClonedMember(programType), result.ClonedTopLevelTypes); foreach (var type in result.ClonedTopLevelTypes) { targetModule.TopLevelTypes.Add(type); } targetModule.ManagedEntrypointMethod = (MethodDefinition)result.ClonedMembers.First(m => m.Name == "Main"); _fixture .GetRunner <FrameworkPERunner>() .RebuildAndRun(targetModule, "HelloWorld.exe", "Hello World!" + Environment.NewLine); }
private static MethodDefinition CloneMethod(MethodBase methodBase, out MethodDefinition originalMethodDef) { var sourceModule = ModuleDefinition.FromFile(methodBase.Module.Assembly.Location); originalMethodDef = (MethodDefinition)sourceModule.LookupMember(methodBase.MetadataToken); var targetModule = PrepareTempModule(); var result = new MemberCloner(targetModule) .Include(originalMethodDef) .Clone(); var clonedMethod = (MethodDefinition)result.ClonedMembers.First(); Assert.True(result.ContainsClonedMember(originalMethodDef)); Assert.Equal(clonedMethod, result.GetClonedMember(originalMethodDef)); return(clonedMethod); }
private static FieldDefinition CloneIntializerField(FieldInfo field, out FieldDefinition originalFieldDef) { var sourceModule = ModuleDefinition.FromFile(field.Module.Assembly.Location); originalFieldDef = (FieldDefinition)sourceModule.LookupMember(field.MetadataToken); originalFieldDef = originalFieldDef.FindInitializerField(); var targetModule = PrepareTempModule(); var result = new MemberCloner(targetModule) .Include(originalFieldDef) .Clone(); var clonedField = (FieldDefinition)result.ClonedMembers.First(); Assert.True(result.ContainsClonedMember(originalFieldDef)); Assert.Equal(clonedField, result.GetClonedMember(originalFieldDef)); return(clonedField); }
private static TypeDefinition CloneType(Type type, out TypeDefinition originalTypeDef) { var sourceModule = ModuleDefinition.FromFile(type.Module.Assembly.Location); originalTypeDef = (TypeDefinition)sourceModule.LookupMember(type.MetadataToken); var targetModule = PrepareTempModule(); var result = new MemberCloner(targetModule) .Include(originalTypeDef) .Clone(); var clonedType = result.ClonedMembers .OfType <TypeDefinition>() .First(); Assert.True(result.ContainsClonedMember(originalTypeDef)); Assert.Equal(clonedType, result.GetClonedMember(originalTypeDef)); Assert.Equal(clonedType, result.ClonedTopLevelTypes.First()); return(clonedType); }