protected override void ScanCore(MemberImportingOptions options, List <MemberImporter> importList) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Clones the attributes attribs = ((ICustomAttributeProvider)Member).CustomAttributes.Select(x => x.Clone()).ToArray(); //Checks the attributes foreach (var x in attribs) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); var a = x; //Imports the type of the attribute var typeImporter = Helpers.CreateTypeImporter(a.AttributeType.Resolve(), Session, importList, options); typeImporter.ImportFinished += (typeRef) => a.Constructor = Helpers.GetConstructorMatchingArguments(((TypeReference)typeRef).Resolve(), a.ConstructorArguments, Session); //Checks if the arguments should be imported for (int i = 0; i < a.ConstructorArguments.Count; i++) { var p = a.ConstructorArguments[i]; //Imports the type of the argument var argumentTypeImporter = Helpers.CreateTypeImporter(p.Type.Resolve(), Session, importList, options); var index = i; argumentTypeImporter.ImportFinished += (typeRef) => { a.ConstructorArguments.RemoveAt(index); a.ConstructorArguments.Insert(index, new CustomAttributeArgument((TypeReference)typeRef, p.Value)); }; } } }
protected override void ScanCore(MemberImportingOptions options, List<MemberImporter> importList) { //Type var type = (TypeSpecification)Member; //Checks if the type is a generic instance type MemberImporter importer; if (type is GenericInstanceType) { importer = new GenericInstanceTypeImporter(type, Session.Destination, Session).Scan(options); importList.Add(importer); importer.ImportFinished += t => retType = (TypeSpecification)t; return; } //Creates the importer for the element type importer = Helpers.CreateTypeImporter(type.ElementType, Session, importList, options); //Switches on the metadata type of the specification switch (type.MetadataType) { case MetadataType.Pointer: importer.ImportFinished += t => retType = new PointerType((TypeReference)t); break; case MetadataType.ByReference: importer.ImportFinished += t => retType = new ByReferenceType((TypeReference)t); break; default: throw new NotImplementedException(); } }
protected override void ScanCore(MemberImportingOptions options, List<MemberImporter> importList) { //Clones the generic parameters parametersClone = ((IGenericParameterProvider)Member).GenericParameters.Select(x => x.Clone((IGenericParameterProvider)Destination, Session)).ToArray(); //For each parameter foreach (var p in parametersClone) { //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); //Registers importing of custom attributes var param = p; if (param.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(p, p, Session).Scan(options)); param.CustomAttributes.Clear(); } //For each constraint creates registers a type importer foreach (var c in param.Constraints.Where(x => !(x is GenericParameter))) { var importer = Helpers.CreateTypeImporter(c, Session, importList, options); importer.ImportFinished += (constraint) => param.Constraints.Add((TypeReference)constraint); } foreach (var x in param.Constraints.Where(x => !(x is GenericParameter)).ToArray()) param.Constraints.Remove(x); } }
protected override void ScanCore(MemberImportingOptions options, List <MemberImporter> importList) { //Clones the generic parameters parametersClone = ((IGenericParameterProvider)Member).GenericParameters.Select(x => x.Clone((IGenericParameterProvider)Destination, Session)).ToArray(); //For each parameter foreach (var p in parametersClone) { //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); //Registers importing of custom attributes var param = p; if (param.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(p, p, Session).Scan(options)); param.CustomAttributes.Clear(); } //For each constraint creates registers a type importer foreach (var c in param.Constraints.Where(x => !(x is GenericParameter))) { var importer = Helpers.CreateTypeImporter(c, Session, importList, options); importer.ImportFinished += (constraint) => param.Constraints.Add((TypeReference)constraint); } foreach (var x in param.Constraints.Where(x => !(x is GenericParameter)).ToArray()) { param.Constraints.Remove(x); } } }
protected override void ScanCore(MemberImportingOptions options, List <MemberImporter> importList) { //Type var type = (TypeSpecification)Member; //Checks if the type is a generic instance type MemberImporter importer; if (type is GenericInstanceType) { importer = new GenericInstanceTypeImporter(type, Session.Destination, Session).Scan(options); importList.Add(importer); importer.ImportFinished += t => retType = (TypeSpecification)t; return; } //Creates the importer for the element type importer = Helpers.CreateTypeImporter(type.ElementType, Session, importList, options); //Switches on the metadata type of the specification switch (type.MetadataType) { case MetadataType.Pointer: importer.ImportFinished += t => retType = new PointerType((TypeReference)t); break; case MetadataType.ByReference: importer.ImportFinished += t => retType = new ByReferenceType((TypeReference)t); break; default: throw new NotImplementedException(); } }
protected override IMetadataTokenProvider ImportCore(MemberImportingOptions options, SharpTreeNode node) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Imports and returns return Session.DestinationModule.Import((TypeReference)Member); }
protected override IMetadataTokenProvider ImportCore(MemberImportingOptions options, SharpTreeNode node) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Imports and returns return(Session.DestinationModule.Import((TypeReference)Member)); }
protected override Mono.Cecil.IMetadataTokenProvider ImportCore(MemberImportingOptions options, ICSharpCode.TreeView.SharpTreeNode node) { //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); //Imports the generic parameters foreach (var x in parametersClone) ((IGenericParameterProvider)Destination).GenericParameters.Add(x); //Returns null return null; }
protected override IMetadataTokenProvider ImportCore(MemberImportingOptions options, SharpTreeNode node) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Adds the field to the destination type ((TypeDefinition)Destination).Fields.Add(fieldClone); if (_createNode) node.AddChildAndColorAncestors(new ILEditTreeNode(fieldClone, false)); //Returns the new field return fieldClone; }
protected override Mono.Cecil.IMetadataTokenProvider ImportCore(MemberImportingOptions options, ICSharpCode.TreeView.SharpTreeNode node) { //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); //Imports the generic parameters foreach (var x in parametersClone) { ((IGenericParameterProvider)Destination).GenericParameters.Add(x); } //Returns null return(null); }
protected override IMetadataTokenProvider ImportCore(MemberImportingOptions options, SharpTreeNode node) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Adds the field to the destination type ((TypeDefinition)Destination).Fields.Add(fieldClone); if (_createNode) { node.AddChildAndColorAncestors(new ILEditTreeNode(fieldClone, false)); } //Returns the new field return(fieldClone); }
protected override Mono.Cecil.IMetadataTokenProvider ImportCore(MemberImportingOptions options, SharpTreeNode node) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Destination var dest = (ICustomAttributeProvider)Destination; //Adds the attributes dest.CustomAttributes.Clear(); foreach (var a in attribs) { dest.CustomAttributes.Add(a); } //Returns null return(null); }
protected override IMetadataTokenProvider ImportCore(MemberImportingOptions options, SharpTreeNode node) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Assembly and module var asm = (AssemblyNameReference)Member; var module = ((ModuleDefinition)Destination); //Adds the reference only if it doesn't already exist module.AssemblyReferences.Add(asm); Helpers.Tree.GetModuleNode(module) .Children.EnsureLazyChildren().FirstOrDefault(x => x is ReferenceFolderTreeNode) .AddChildAndColorAncestors(new ILEditTreeNode(asm, true)); //Returns null return(null); }
protected override IMetadataTokenProvider ImportCore(MemberImportingOptions options, SharpTreeNode node) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Assembly and module var asm = (AssemblyNameReference)Member; var module = ((ModuleDefinition)Destination); //Adds the reference only if it doesn't already exist module.AssemblyReferences.Add(asm); Helpers.Tree.GetModuleNode(module) .Children.EnsureLazyChildren().FirstOrDefault(x => x is ReferenceFolderTreeNode) .AddChildAndColorAncestors(new ILEditTreeNode(asm, true)); //Returns null return null; }
protected override void ScanCore(MemberImportingOptions options, List<MemberImporter> importList) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Field fieldClone = ((FieldDefinition)Member).Clone(Session); //Field type var fieldType = fieldClone.FieldType; //Imports the type var typeImporter = Helpers.CreateTypeImporter(fieldType, Session, importList, options); typeImporter.ImportFinished += (typeRef) => fieldClone.FieldType = (TypeReference)typeRef; //Checks the attributes of the field if (fieldClone.HasCustomAttributes) importList.Add(new CustomAttributesImporter(fieldClone, fieldClone, Session).Scan(options)); }
protected override void ScanCore(MemberImportingOptions options, List <MemberImporter> importList) { //Type var type = (GenericInstanceType)Member; //Element type var elType = type.ElementType.Resolve(); retType = new GenericInstanceType(elType); var elTypeImporter = Helpers.CreateTypeImporter(elType, Session, importList, options); elTypeImporter.ImportFinished += t => { var newType = new GenericInstanceType((TypeReference)t); foreach (var a in retType.GenericArguments) { newType.GenericArguments.Add(a); } retType = newType; }; //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); //Imports the arguments foreach (var a in type.GenericArguments) { var arg = a; if (a is GenericParameter) { importList.Add(MemberImporter.Create((_, __) => { retType.GenericArguments.Add(arg); return(null); })); } else { var argImporter = Helpers.CreateTypeImporter(a, Session, importList, options); argImporter.ImportFinished += x => retType.GenericArguments.Add((TypeReference)x); } } //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); }
protected override IMetadataTokenProvider ImportCore(MemberImportingOptions options, SharpTreeNode node) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Checks if the destination is a module or a type if (Destination is ModuleDefinition) { //Destination var dest = (ModuleDefinition)Destination; dest.Types.Add(typeClone); //Finds the correct namespace var ns = typeClone.Namespace; var moduleNode = Helpers.Tree.GetModuleNode((ModuleDefinition)Destination); var nsNode = moduleNode.Children.EnsureLazyChildren().OfType <NamespaceTreeNode>().FirstOrDefault(x => x.Name == ns); if (nsNode != null) { nsNode.AddChildAndColorAncestors(new ILEditTreeNode(typeClone, false)); } else { nsNode = new NamespaceTreeNode(typeClone.Namespace) { Foreground = GlobalContainer.ModifiedNodesBrush }; nsNode.Children.Add(new ILEditTreeNode(typeClone, false)); moduleNode.AddChildAndColorAncestors(nsNode); } } else { //Destination var dest = (TypeDefinition)Destination; dest.NestedTypes.Add(typeClone); node.AddChildAndColorAncestors(new ILEditTreeNode(typeClone, false)); } //Return return(typeClone); }
protected override void ScanCore(MemberImportingOptions options, List <MemberImporter> importList) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Field fieldClone = ((FieldDefinition)Member).Clone(Session); //Field type var fieldType = fieldClone.FieldType; //Imports the type var typeImporter = Helpers.CreateTypeImporter(fieldType, Session, importList, options); typeImporter.ImportFinished += (typeRef) => fieldClone.FieldType = (TypeReference)typeRef; //Checks the attributes of the field if (fieldClone.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(fieldClone, fieldClone, Session).Scan(options)); } }
protected override void ScanCore(MemberImportingOptions options, List<MemberImporter> importList) { //Type var type = (GenericInstanceType)Member; //Element type var elType = type.ElementType.Resolve(); retType = new GenericInstanceType(elType); var elTypeImporter = Helpers.CreateTypeImporter(elType, Session, importList, options); elTypeImporter.ImportFinished += t => { var newType = new GenericInstanceType((TypeReference)t); foreach (var a in retType.GenericArguments) newType.GenericArguments.Add(a); retType = newType; }; //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); //Imports the arguments foreach (var a in type.GenericArguments) { var arg = a; if (a is GenericParameter) { importList.Add(MemberImporter.Create((_, __) => { retType.GenericArguments.Add(arg); return null; })); } else { var argImporter = Helpers.CreateTypeImporter(a, Session, importList, options); argImporter.ImportFinished += x => retType.GenericArguments.Add((TypeReference)x); } } //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); }
protected override void ScanCore(MemberImportingOptions options, List<MemberImporter> importList) { //Method var originalMethod = (MethodDefinition)Member; methodClone = originalMethod.Clone(Session); //Imports the generic parameters if (methodClone.HasGenericParameters) { importList.Add(new GenericParametersImporter(methodClone, methodClone, Session).Scan(options)); methodClone.GenericParameters.Clear(); } //Imports the attributes if (methodClone.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(methodClone, methodClone, Session).Scan(options)); methodClone.CustomAttributes.Clear(); } //Imports the return type var retImporter = Helpers.CreateTypeImporter(originalMethod.ReturnType, Session, importList, options); retImporter.ImportFinished += t => methodClone.ReturnType = (TypeReference)t; //Imports the attributes of the return type if (methodClone.MethodReturnType.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(methodClone.MethodReturnType, methodClone.MethodReturnType, Session).Scan(options)); methodClone.MethodReturnType.CustomAttributes.Clear(); } //Imports the parameters foreach (var p in originalMethod.Parameters) { //Creates a new parameter var param = new ParameterDefinition(p.Name, p.Attributes, p.ParameterType) { Constant = p.Constant, MarshalInfo = p.MarshalInfo, MetadataToken = new MetadataToken(p.MetadataToken.TokenType, GlobalContainer.GetFreeRID(Session.DestinationModule)) }; methodClone.Parameters.Add(param); //Queues importing of custom attributes if (p.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(p, param, Session).Scan(options)); param.CustomAttributes.Clear(); } //Queues importing of type var typeImporter = Helpers.CreateTypeImporter(p.ParameterType, Session, importList, options); typeImporter.ImportFinished += t => param.ParameterType = (TypeReference)t; } //Clones the body var originalBody = originalMethod.Body; var body = methodClone.Body; body.InitLocals = originalBody.InitLocals; body.MaxStackSize = originalBody.MaxStackSize; //Clones the variables foreach (var v in originalBody.Variables) { var var = new VariableDefinition(v.Name, v.VariableType); var typeImporter = Helpers.CreateTypeImporter(var.VariableType, Session, importList, options); typeImporter.ImportFinished += t => var.VariableType = (TypeReference)t; body.Variables.Add(var); } //Clones the instructions foreach (var x in originalBody.Instructions) { //Creates a new instruction with the same opcode var i = x; var instruction = Instruction.Create(OpCodes.Nop); instruction.OpCode = i.OpCode; body.Instructions.Add(instruction); instruction.Offset = i.Offset; //Switches on the type of the opcode to switch (i.OpCode.OperandType) { //Delays the importing of the operand case OperandType.ShortInlineBrTarget: case OperandType.InlineBrTarget: var index = originalBody.Instructions.IndexOf((Instruction)i.Operand); importList.Add(MemberImporter.Create((_, __) => { instruction.Operand = body.Instructions[index]; return null; })); break; case OperandType.InlineSwitch: var indexes = ((Instruction[])i.Operand).Select(a => originalBody.Instructions.IndexOf(a)).ToArray(); importList.Add(MemberImporter.Create((_, __) => { instruction.Operand = indexes.Select(a => body.Instructions[a]).ToArray(); return null; })); break; //Registers importing of the type case OperandType.InlineType: var importer = Helpers.CreateTypeImporter((TypeReference)i.Operand, Session, importList, options); importer.ImportFinished += t => instruction.Operand = t; break; //Registers importing of the declaring type //along with the field type case OperandType.InlineField: var field = (FieldReference)i.Operand; field = new FieldReference(field.Name, field.FieldType, field.DeclaringType); Helpers.CreateTypeImporter(field.DeclaringType, Session, importList, options) .ImportFinished += t => field.DeclaringType = (TypeReference)t; Helpers.CreateTypeImporter(field.FieldType, Session, importList, options) .ImportFinished += t => field.FieldType = (TypeReference)t; instruction.Operand = field; break; //Registers importing of the method case OperandType.InlineMethod: var m = (MethodReference)i.Operand; var methodImporter = MemberImporter.Create((_, __) => { instruction.Operand = Session.DestinationModule.Import(m); return null; }); importList.Add(methodImporter); break; //Keeps the same operand default: instruction.Operand = i.Operand; break; } } //Imports the overrides if (methodClone.HasOverrides) { foreach (var x in methodClone.Overrides) { var o = x; importList.Add(MemberImporter.Create((_, __) => { methodClone.Overrides.Add(Session.DestinationModule.Import(o)); return null; })); } methodClone.Overrides.Clear(); } }
protected override void ScanCore(MemberImportingOptions options, List<MemberImporter> importList) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); }
protected override Mono.Cecil.IMetadataTokenProvider ImportCore(MemberImportingOptions options, ICSharpCode.TreeView.SharpTreeNode node) { //Returns the type return(retType); }
protected override void ScanCore(MemberImportingOptions options, List<MemberImporter> importList) { //Property var originalProp = (PropertyDefinition)Member; propClone = originalProp.Clone(Session); //Registers importing of custom attributes if (propClone.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(propClone, propClone, Session).Scan(options)); propClone.CustomAttributes.Clear(); } //Registers importing of get and set methods if (propClone.GetMethod != null) { var importer = new MethodImporter(propClone.GetMethod, Destination, Session, false).Scan(options); importer.ImportFinished += m => { var get = (MethodDefinition)m; get.IsGetter = true; propClone.GetMethod = get; }; importList.Add(importer); } if (propClone.SetMethod != null) { var importer = new MethodImporter(propClone.SetMethod, Destination, Session, false).Scan(options); importer.ImportFinished += m => { var set = (MethodDefinition)m; set.IsSetter = true; propClone.SetMethod = set; }; importList.Add(importer); } //Imports other methods if (originalProp.HasOtherMethods) { foreach (var m in originalProp.OtherMethods) { var importer = new MethodImporter(m, Destination, Session, false).Scan(options); importer.ImportFinished += x => { var method = (MethodDefinition)x; method.IsOther = true; propClone.OtherMethods.Add(method); }; importList.Add(importer); } } //Imports the parameters foreach (var p in originalProp.Parameters) { //Creates a new parameter var param = new ParameterDefinition(p.Name, p.Attributes, p.ParameterType) { Constant = p.Constant, MarshalInfo = p.MarshalInfo, MetadataToken = new MetadataToken(p.MetadataToken.TokenType, GlobalContainer.GetFreeRID(Session.DestinationModule)) }; propClone.Parameters.Add(param); //Queues importing of custom attributes if (p.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(p, param, Session).Scan(options)); param.CustomAttributes.Clear(); } //Queues importing of type var typeImporter = Helpers.CreateTypeImporter(p.ParameterType, Session, importList, options); typeImporter.ImportFinished += t => param.ParameterType = (TypeReference)t; } }
protected override void ScanCore(MemberImportingOptions options, List<MemberImporter> importList) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Clones the type typeClone = ((TypeDefinition)Member).Clone(Session); //Adjusts the visibility of the type if (Destination.MetadataToken.TokenType == TokenType.Module) { //Makes sure that the type isn't marked as nested var visibility = typeClone.Attributes & TypeAttributes.VisibilityMask; typeClone.Attributes &= ~TypeAttributes.VisibilityMask; switch (visibility) { case TypeAttributes.Public: case TypeAttributes.NestedPublic: typeClone.Attributes |= TypeAttributes.Public; break; case TypeAttributes.NotPublic: case TypeAttributes.NestedPrivate: case TypeAttributes.NestedFamily: case TypeAttributes.NestedAssembly: case TypeAttributes.NestedFamANDAssem: case TypeAttributes.NestedFamORAssem: typeClone.Attributes |= TypeAttributes.NotPublic; break; } } else { //Makes sure that the type is marked as nested if (typeClone.IsPublic) { typeClone.Attributes = typeClone.Attributes & ~TypeAttributes.VisibilityMask | TypeAttributes.NestedPublic; } else if (typeClone.IsNotPublic) { typeClone.Attributes = typeClone.Attributes & ~TypeAttributes.VisibilityMask | TypeAttributes.NestedAssembly; } } //Registers the importing of the custom attributes of this class if (typeClone.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(typeClone, typeClone, Session).Scan(options)); typeClone.CustomAttributes.Clear(); } //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); //Registers importing of generic parameters constraints if (typeClone.HasGenericParameters) { importList.Add(new GenericParametersImporter(typeClone, typeClone, Session).Scan(options)); typeClone.GenericParameters.Clear(); } //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); //Registers importing of base type if (typeClone.BaseType != null && typeClone.BaseType.FullName != "System.Object") { var baseTypeImporter = Helpers.CreateTypeImporter(typeClone.BaseType, Session, importList, options); baseTypeImporter.ImportFinished += t => typeClone.BaseType = (TypeReference)t; } //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); //Registers the importing of the members var type = (TypeDefinition)Member; var importers = type.Fields.Select(f => new FieldImporter(f, typeClone, Session, false).Scan(options)) .Concat( type.Methods .Where(m => !(m.IsGetter || m.IsSetter || m.IsAddOn || m.IsRemoveOn || m.IsFire || m.IsOther)) .Select(m => new MethodImporter(m, typeClone, Session, false).Scan(options)) ).Concat(type.Properties.Select(p => new PropertyImporter(p, typeClone, Session, false).Scan(options))) .Concat(type.Events.Select(e => new EventImporter(e, typeClone, Session, false).Scan(options))); foreach (var x in importers) { options.CancellationToken.ThrowIfCancellationRequested(); importList.Add(x); } //TODO: other members }
protected override void ScanCore(MemberImportingOptions options, List <MemberImporter> importList) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); }
protected override void ScanCore(MemberImportingOptions options, List <MemberImporter> importList) { //Method var originalMethod = (MethodDefinition)Member; methodClone = originalMethod.Clone(Session); //Imports the generic parameters if (methodClone.HasGenericParameters) { importList.Add(new GenericParametersImporter(methodClone, methodClone, Session).Scan(options)); methodClone.GenericParameters.Clear(); } //Imports the attributes if (methodClone.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(methodClone, methodClone, Session).Scan(options)); methodClone.CustomAttributes.Clear(); } //Imports the return type var retImporter = Helpers.CreateTypeImporter(originalMethod.ReturnType, Session, importList, options); retImporter.ImportFinished += t => methodClone.ReturnType = (TypeReference)t; //Imports the attributes of the return type if (methodClone.MethodReturnType.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(methodClone.MethodReturnType, methodClone.MethodReturnType, Session).Scan(options)); methodClone.MethodReturnType.CustomAttributes.Clear(); } //Imports the parameters foreach (var p in originalMethod.Parameters) { //Creates a new parameter var param = new ParameterDefinition(p.Name, p.Attributes, p.ParameterType) { Constant = p.Constant, MarshalInfo = p.MarshalInfo, MetadataToken = new MetadataToken(p.MetadataToken.TokenType, GlobalContainer.GetFreeRID(Session.DestinationModule)) }; methodClone.Parameters.Add(param); //Queues importing of custom attributes if (p.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(p, param, Session).Scan(options)); param.CustomAttributes.Clear(); } //Queues importing of type var typeImporter = Helpers.CreateTypeImporter(p.ParameterType, Session, importList, options); typeImporter.ImportFinished += t => param.ParameterType = (TypeReference)t; } //Clones the body var originalBody = originalMethod.Body; var body = methodClone.Body; body.InitLocals = originalBody.InitLocals; body.MaxStackSize = originalBody.MaxStackSize; //Clones the variables foreach (var v in originalBody.Variables) { var var = new VariableDefinition(v.Name, v.VariableType); var typeImporter = Helpers.CreateTypeImporter(var.VariableType, Session, importList, options); typeImporter.ImportFinished += t => var.VariableType = (TypeReference)t; body.Variables.Add(var); } //Clones the instructions foreach (var x in originalBody.Instructions) { //Creates a new instruction with the same opcode var i = x; var instruction = Instruction.Create(OpCodes.Nop); instruction.OpCode = i.OpCode; body.Instructions.Add(instruction); instruction.Offset = i.Offset; //Switches on the type of the opcode to switch (i.OpCode.OperandType) { //Delays the importing of the operand case OperandType.ShortInlineBrTarget: case OperandType.InlineBrTarget: var index = originalBody.Instructions.IndexOf((Instruction)i.Operand); importList.Add(MemberImporter.Create((_, __) => { instruction.Operand = body.Instructions[index]; return(null); })); break; case OperandType.InlineSwitch: var indexes = ((Instruction[])i.Operand).Select(a => originalBody.Instructions.IndexOf(a)).ToArray(); importList.Add(MemberImporter.Create((_, __) => { instruction.Operand = indexes.Select(a => body.Instructions[a]).ToArray(); return(null); })); break; //Registers importing of the type case OperandType.InlineType: var importer = Helpers.CreateTypeImporter((TypeReference)i.Operand, Session, importList, options); importer.ImportFinished += t => instruction.Operand = t; break; //Registers importing of the declaring type //along with the field type case OperandType.InlineField: var field = (FieldReference)i.Operand; field = new FieldReference(field.Name, field.FieldType, field.DeclaringType); Helpers.CreateTypeImporter(field.DeclaringType, Session, importList, options) .ImportFinished += t => field.DeclaringType = (TypeReference)t; Helpers.CreateTypeImporter(field.FieldType, Session, importList, options) .ImportFinished += t => field.FieldType = (TypeReference)t; instruction.Operand = field; break; //Registers importing of the method case OperandType.InlineMethod: var m = (MethodReference)i.Operand; var methodImporter = MemberImporter.Create((_, __) => { instruction.Operand = Session.DestinationModule.Import(m); return(null); }); importList.Add(methodImporter); break; //Keeps the same operand default: instruction.Operand = i.Operand; break; } } //Imports the overrides if (methodClone.HasOverrides) { foreach (var x in methodClone.Overrides) { var o = x; importList.Add(MemberImporter.Create((_, __) => { methodClone.Overrides.Add(Session.DestinationModule.Import(o)); return(null); })); } methodClone.Overrides.Clear(); } }
private static MemberImporter CreateTypeImporterForTypeDefinition(TypeDefinition type, MemberImportingSession session, List <MemberImporter> importList, MemberImportingOptions options) { //Checks if the type is accessible if ( (session.DestinationType != null && Helpers.IsTypeAccessibleFrom(type, session.DestinationType)) || Helpers.IsTypeAccessibleFrom(type, session.DestinationModule) ) { //Queues addition of an assembly reference if (type.Module != session.DestinationModule) { if (!session.DestinationModule.AssemblyReferences.Any(x => x.FullName == type.Module.Assembly.Name.FullName)) { //Checks if the reference importer is already in the dictionary MemberImporter importer; if (!session.RegisteredImporters.TryGetValue(type.Module.Assembly.Name, out importer)) { importList.Add(importer = new AssemblyReferenceImporter(type.Module.Assembly.Name, session).Scan(options)); session.RegisteredImporters.Add(type.Module.Assembly.Name, importer); } } } //Creates the type importer return(new TypeReferenceInModuleImporter(type, session)); } else { //Creates the type importer return(new TypeImporter(type, options.ImportAsNestedType ? (IMetadataTokenProvider)session.DestinationType : (IMetadataTokenProvider)session.DestinationModule, session)); } }
protected override IMetadataTokenProvider ImportCore(MemberImportingOptions options, SharpTreeNode node) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Checks if the destination is a module or a type if (Destination is ModuleDefinition) { //Destination var dest = (ModuleDefinition)Destination; dest.Types.Add(typeClone); //Finds the correct namespace var ns = typeClone.Namespace; var moduleNode = Helpers.Tree.GetModuleNode((ModuleDefinition)Destination); var nsNode = moduleNode.Children.EnsureLazyChildren().OfType<NamespaceTreeNode>().FirstOrDefault(x => x.Name == ns); if (nsNode != null) nsNode.AddChildAndColorAncestors(new ILEditTreeNode(typeClone, false)); else { nsNode = new NamespaceTreeNode(typeClone.Namespace) { Foreground = GlobalContainer.ModifiedNodesBrush }; nsNode.Children.Add(new ILEditTreeNode(typeClone, false)); moduleNode.AddChildAndColorAncestors(nsNode); } } else { //Destination var dest = (TypeDefinition)Destination; dest.NestedTypes.Add(typeClone); node.AddChildAndColorAncestors(new ILEditTreeNode(typeClone, false)); } //Return return typeClone; }
protected override void ScanCore(MemberImportingOptions options, List<MemberImporter> importList) { //Event var originalEvt = (EventDefinition)Member; evtClone = originalEvt.Clone(Session); //Registers importing of custom attributes if (evtClone.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(evtClone, evtClone, Session).Scan(options)); evtClone.CustomAttributes.Clear(); } //Registers importing of add, remove and invoke methods if (originalEvt.AddMethod != null) { var importer = new MethodImporter(originalEvt.AddMethod, Destination, Session, false).Scan(options); importer.ImportFinished += m => { var add = (MethodDefinition)m; add.IsAddOn = true; evtClone.AddMethod = add; }; importList.Add(importer); } if (originalEvt.RemoveMethod != null) { var importer = new MethodImporter(originalEvt.RemoveMethod, Destination, Session, false).Scan(options); importer.ImportFinished += m => { var remove = (MethodDefinition)m; remove.IsRemoveOn = true; evtClone.RemoveMethod = remove; }; importList.Add(importer); } if (originalEvt.InvokeMethod != null) { var importer = new MethodImporter(originalEvt.InvokeMethod, Destination, Session, false).Scan(options); importer.ImportFinished += m => { var invoke = (MethodDefinition)m; invoke.IsFire = true; evtClone.InvokeMethod = invoke; }; importList.Add(importer); } //Imports other methods if (originalEvt.HasOtherMethods) { foreach (var m in originalEvt.OtherMethods) { var importer = new MethodImporter(m, Destination, Session, false).Scan(options); importer.ImportFinished += x => { var method = (MethodDefinition)x; method.IsOther = true; evtClone.OtherMethods.Add(method); }; importList.Add(importer); } } }
protected override void ScanCore(MemberImportingOptions options, List <MemberImporter> importList) { //Property var originalProp = (PropertyDefinition)Member; propClone = originalProp.Clone(Session); //Registers importing of custom attributes if (propClone.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(propClone, propClone, Session).Scan(options)); propClone.CustomAttributes.Clear(); } //Registers importing of get and set methods if (propClone.GetMethod != null) { var importer = new MethodImporter(propClone.GetMethod, Destination, Session, false).Scan(options); importer.ImportFinished += m => { var get = (MethodDefinition)m; get.IsGetter = true; propClone.GetMethod = get; }; importList.Add(importer); } if (propClone.SetMethod != null) { var importer = new MethodImporter(propClone.SetMethod, Destination, Session, false).Scan(options); importer.ImportFinished += m => { var set = (MethodDefinition)m; set.IsSetter = true; propClone.SetMethod = set; }; importList.Add(importer); } //Imports other methods if (originalProp.HasOtherMethods) { foreach (var m in originalProp.OtherMethods) { var importer = new MethodImporter(m, Destination, Session, false).Scan(options); importer.ImportFinished += x => { var method = (MethodDefinition)x; method.IsOther = true; propClone.OtherMethods.Add(method); }; importList.Add(importer); } } //Imports the parameters foreach (var p in originalProp.Parameters) { //Creates a new parameter var param = new ParameterDefinition(p.Name, p.Attributes, p.ParameterType) { Constant = p.Constant, MarshalInfo = p.MarshalInfo, MetadataToken = new MetadataToken(p.MetadataToken.TokenType, GlobalContainer.GetFreeRID(Session.DestinationModule)) }; propClone.Parameters.Add(param); //Queues importing of custom attributes if (p.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(p, param, Session).Scan(options)); param.CustomAttributes.Clear(); } //Queues importing of type var typeImporter = Helpers.CreateTypeImporter(p.ParameterType, Session, importList, options); typeImporter.ImportFinished += t => param.ParameterType = (TypeReference)t; } }
protected override Mono.Cecil.IMetadataTokenProvider ImportCore(MemberImportingOptions options, ICSharpCode.TreeView.SharpTreeNode node) { //Returns the type return retType; }
protected override void ScanCore(MemberImportingOptions options, List <MemberImporter> importList) { //Event var originalEvt = (EventDefinition)Member; evtClone = originalEvt.Clone(Session); //Registers importing of custom attributes if (evtClone.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(evtClone, evtClone, Session).Scan(options)); evtClone.CustomAttributes.Clear(); } //Registers importing of add, remove and invoke methods if (originalEvt.AddMethod != null) { var importer = new MethodImporter(originalEvt.AddMethod, Destination, Session, false).Scan(options); importer.ImportFinished += m => { var add = (MethodDefinition)m; add.IsAddOn = true; evtClone.AddMethod = add; }; importList.Add(importer); } if (originalEvt.RemoveMethod != null) { var importer = new MethodImporter(originalEvt.RemoveMethod, Destination, Session, false).Scan(options); importer.ImportFinished += m => { var remove = (MethodDefinition)m; remove.IsRemoveOn = true; evtClone.RemoveMethod = remove; }; importList.Add(importer); } if (originalEvt.InvokeMethod != null) { var importer = new MethodImporter(originalEvt.InvokeMethod, Destination, Session, false).Scan(options); importer.ImportFinished += m => { var invoke = (MethodDefinition)m; invoke.IsFire = true; evtClone.InvokeMethod = invoke; }; importList.Add(importer); } //Imports other methods if (originalEvt.HasOtherMethods) { foreach (var m in originalEvt.OtherMethods) { var importer = new MethodImporter(m, Destination, Session, false).Scan(options); importer.ImportFinished += x => { var method = (MethodDefinition)x; method.IsOther = true; evtClone.OtherMethods.Add(method); }; importList.Add(importer); } } }
protected override void ScanCore(MemberImportingOptions options, List <MemberImporter> importList) { //Checks that the task hasn't been canceled options.CancellationToken.ThrowIfCancellationRequested(); //Clones the type typeClone = ((TypeDefinition)Member).Clone(Session); //Adjusts the visibility of the type if (Destination.MetadataToken.TokenType == TokenType.Module) { //Makes sure that the type isn't marked as nested var visibility = typeClone.Attributes & TypeAttributes.VisibilityMask; typeClone.Attributes &= ~TypeAttributes.VisibilityMask; switch (visibility) { case TypeAttributes.Public: case TypeAttributes.NestedPublic: typeClone.Attributes |= TypeAttributes.Public; break; case TypeAttributes.NotPublic: case TypeAttributes.NestedPrivate: case TypeAttributes.NestedFamily: case TypeAttributes.NestedAssembly: case TypeAttributes.NestedFamANDAssem: case TypeAttributes.NestedFamORAssem: typeClone.Attributes |= TypeAttributes.NotPublic; break; } } else { //Makes sure that the type is marked as nested if (typeClone.IsPublic) { typeClone.Attributes = typeClone.Attributes & ~TypeAttributes.VisibilityMask | TypeAttributes.NestedPublic; } else if (typeClone.IsNotPublic) { typeClone.Attributes = typeClone.Attributes & ~TypeAttributes.VisibilityMask | TypeAttributes.NestedAssembly; } } //Registers the importing of the custom attributes of this class if (typeClone.HasCustomAttributes) { importList.Add(new CustomAttributesImporter(typeClone, typeClone, Session).Scan(options)); typeClone.CustomAttributes.Clear(); } //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); //Registers importing of generic parameters constraints if (typeClone.HasGenericParameters) { importList.Add(new GenericParametersImporter(typeClone, typeClone, Session).Scan(options)); typeClone.GenericParameters.Clear(); } //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); //Registers importing of base type if (typeClone.BaseType != null && typeClone.BaseType.FullName != "System.Object") { var baseTypeImporter = Helpers.CreateTypeImporter(typeClone.BaseType, Session, importList, options); baseTypeImporter.ImportFinished += t => typeClone.BaseType = (TypeReference)t; } //Throws if cancellation was requested options.CancellationToken.ThrowIfCancellationRequested(); //Registers the importing of the members var type = (TypeDefinition)Member; var importers = type.Fields.Select(f => new FieldImporter(f, typeClone, Session, false).Scan(options)) .Concat( type.Methods .Where(m => !(m.IsGetter || m.IsSetter || m.IsAddOn || m.IsRemoveOn || m.IsFire || m.IsOther)) .Select(m => new MethodImporter(m, typeClone, Session, false).Scan(options)) ).Concat(type.Properties.Select(p => new PropertyImporter(p, typeClone, Session, false).Scan(options))) .Concat(type.Events.Select(e => new EventImporter(e, typeClone, Session, false).Scan(options))); foreach (var x in importers) { options.CancellationToken.ThrowIfCancellationRequested(); importList.Add(x); } //TODO: other members }
/// <summary> /// Creates a new MemberImporter to import a type to destination, automatically adding any other required importer /// </summary> /// <param name="type"></param> /// <param name="destType"></param> /// <param name="importList"></param> /// <param name="options"></param> /// <returns></returns> public static MemberImporter CreateTypeImporter(TypeReference type, MemberImportingSession session, List <MemberImporter> importList, MemberImportingOptions options) { //Return value MemberImporter ret; //Checks if the type is a generic instance type or a generic parameter if (type is GenericParameter) { ret = MemberImporter.Create((_, __) => type); } else if (type is TypeSpecification) { ret = new TypeSpecificationImporter((TypeSpecification)type, session).Scan(options); } else { //Checks if the importer is in the dictionary MemberImporter importer; if (session.RegisteredImporters.TryGetValue(type, out importer)) { return(importer); } //Creates the importer, registers it and returns importer = Helpers.CreateTypeImporterForTypeDefinition(((TypeReference)type).Resolve(), session, importList, options); session.RegisteredImporters.Add(type, importer); importer.Scan(options); importList.Add(importer); return(importer); } //Adds the return value to the import list and returns importList.Add(ret); return(ret); }