Пример #1
0
 protected override bool CanImportCore(Mono.Cecil.IMetadataTokenProvider member, Mono.Cecil.IMetadataTokenProvider destination)
 {
     return(member.MetadataToken.TokenType == TokenType.TypeDef && (destination.MetadataToken.TokenType == TokenType.TypeDef || destination.MetadataToken.TokenType == TokenType.Module));
 }
Пример #2
0
 protected override bool CanImportCore(Mono.Cecil.IMetadataTokenProvider member, Mono.Cecil.IMetadataTokenProvider destination)
 {
     return(member is AssemblyNameReference && destination is ModuleDefinition);
 }
Пример #3
0
 protected override bool CanImportCore(Mono.Cecil.IMetadataTokenProvider member, Mono.Cecil.IMetadataTokenProvider destination)
 {
     return(member is TypeSpecification);
 }
Пример #4
0
 protected override bool CanImportCore(Mono.Cecil.IMetadataTokenProvider member, Mono.Cecil.IMetadataTokenProvider destination)
 {
     return(member is ICustomAttributeProvider && destination is ICustomAttributeProvider);
 }
Пример #5
0
        public void Inject(ICSharpCode.ILSpy.TreeNodes.ILSpyTreeNode node, string name, Mono.Cecil.IMetadataTokenProvider member)
        {
            //Type
            var type = (TypeDefinition)((IMemberTreeNode)node).Member;

            //Event type
            var eventType = type.Module.Import((TypeReference)member, type);

            //Creates the event
            var evt = new EventDefinition(name, EventAttributes.None, eventType)
            {
                MetadataToken = new MetadataToken(TokenType.Event, GlobalContainer.GetFreeRID(type.Module))
            };

            //Creates the field
            FieldReference backingField = null;

            if (!type.IsInterface)
            {
                backingField = new FieldDefinition(
                    name,
                    FieldAttributes.Private,
                    eventType
                    )
                {
                    MetadataToken = new MetadataToken(TokenType.Field, GlobalContainer.GetFreeRID(type.Module))
                };
                type.Fields.Add((FieldDefinition)backingField);
            }

            //Checks if the type is generic
            if (type.HasGenericParameters)
            {
                var giType = new GenericInstanceType(type);
                foreach (var x in type.GenericParameters)
                {
                    giType.GenericArguments.Add(x);
                }
                backingField = new FieldReference(backingField.Name, eventType, giType);
            }

            //Creates the addon method
            evt.AddMethod = new MethodDefinition(
                "add_" + name,
                MethodAttributes.Public | MethodAttributes.SpecialName,
                type.Module.TypeSystem.Void
                )
            {
                IsSynchronized = !type.IsInterface,
                IsAddOn        = true,
                MetadataToken  = new MetadataToken(TokenType.Method, GlobalContainer.GetFreeRID(type.Module)),
                Parameters     =
                {
                    new ParameterDefinition("value", ParameterAttributes.None, eventType)
                }
            };

            //Checks if the destination type is an interface
            if (type.IsInterface)
            {
                evt.AddMethod.Attributes |= MethodAttributes.NewSlot | MethodAttributes.CheckAccessOnOverride | MethodAttributes.Abstract | MethodAttributes.Virtual;
            }
            else
            {
                //Writes the instruction of the addon method
                var addBody = evt.AddMethod.Body;
                addBody.MaxStackSize = 8;
                addBody.GetType().GetField("code_size", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(addBody, 1);
                var addIL = addBody.GetILProcessor();
                addIL.Emit(OpCodes.Ldarg_0);
                addIL.Emit(OpCodes.Ldarg_0);
                addIL.Emit(OpCodes.Ldfld, backingField);
                addIL.Emit(OpCodes.Ldarg_1);
                //Delegate.Combine(Delegate, Delegate)
                addIL.Emit(OpCodes.Call, type.Module.Import(new TypeReference("System", "Delegate", type.Module, type.Module.TypeSystem.Corlib).Resolve().Methods.First(x => x.Name == "Combine" && x.IsStatic && x.Parameters.Count == 2 && x.Parameters.All(p => p.ParameterType.FullName == "System.Delegate"))));
                addIL.Emit(OpCodes.Castclass, eventType);
                addIL.Emit(OpCodes.Stfld, backingField);
                addIL.Emit(OpCodes.Ret);
            }
            //Creates the removeon method
            evt.RemoveMethod = new MethodDefinition(
                "remove_" + name,
                MethodAttributes.Public | MethodAttributes.SpecialName,
                type.Module.TypeSystem.Void
                )
            {
                IsSynchronized = !type.IsInterface,
                IsRemoveOn     = true,
                MetadataToken  = new MetadataToken(TokenType.Method, GlobalContainer.GetFreeRID(type.Module)),
                Parameters     =
                {
                    new ParameterDefinition("value", ParameterAttributes.None, eventType)
                }
            };

            //Checks if the destination type is an interface
            if (type.IsInterface)
            {
                evt.RemoveMethod.Attributes |= MethodAttributes.NewSlot | MethodAttributes.CheckAccessOnOverride | MethodAttributes.Abstract | MethodAttributes.Virtual;
            }
            else
            {
                //Writes the instruction of the removeon method
                var removeBody = evt.RemoveMethod.Body;
                removeBody.MaxStackSize = 8;
                removeBody.GetType().GetField("code_size", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(removeBody, 1);
                var removeIL = removeBody.GetILProcessor();
                removeIL.Emit(OpCodes.Ldarg_0);
                removeIL.Emit(OpCodes.Ldarg_0);
                removeIL.Emit(OpCodes.Ldfld, backingField);
                removeIL.Emit(OpCodes.Ldarg_1);
                //Delegate.Remove(Delegate, Delegate)
                removeIL.Emit(OpCodes.Call, type.Module.Import(new TypeReference("System", "Delegate", type.Module, type.Module.TypeSystem.Corlib).Resolve().Methods.First(x => x.Name == "Remove" && x.IsStatic && x.Parameters.Count == 2 && x.Parameters.All(p => p.ParameterType.FullName == "System.Delegate"))));
                removeIL.Emit(OpCodes.Castclass, eventType);
                removeIL.Emit(OpCodes.Stfld, backingField);
                removeIL.Emit(OpCodes.Ret);
            }

            //Adds the members to the type
            type.Methods.Add(evt.AddMethod);
            type.Methods.Add(evt.RemoveMethod);
            type.Events.Add(evt);
            evt.AddMethod.Overrides.Clear();
            evt.RemoveMethod.Overrides.Clear();

            //Creates the nodes
            if (node is TypeTreeNode)
            {
                if (!type.IsInterface)
                {
                    node.Children.Add(new ILEditTreeNode(backingField, true));
                }
                node.Children.Add(new ILEditTreeNode(evt, false));
                Helpers.Tree.SortChildren((TypeTreeNode)node);
            }
            else if (node is ILEditTreeNode)
            {
                ((ILEditTreeNode)node).RefreshChildren();
            }
        }
Пример #6
0
 protected override bool CanImportCore(Mono.Cecil.IMetadataTokenProvider member, Mono.Cecil.IMetadataTokenProvider destination)
 {
     return(member.MetadataToken.TokenType == TokenType.Property && destination.MetadataToken.TokenType == TokenType.TypeDef);
 }
 protected override bool CanImportCore(Mono.Cecil.IMetadataTokenProvider member, Mono.Cecil.IMetadataTokenProvider destination)
 {
     return(member is IGenericParameterProvider && destination is IGenericParameterProvider);
 }
 protected override bool CanImportCore(Mono.Cecil.IMetadataTokenProvider member, Mono.Cecil.IMetadataTokenProvider destination)
 {
     return(member is GenericInstanceType);
 }