コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }