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); }