Пример #1
0
        public FieldDefinition resolve(FieldReference field)
        {
            if (field == null)
            {
                return(null);
            }
            var fieldDef = field as FieldDefinition;

            if (fieldDef != null)
            {
                return(fieldDef);
            }

            var type = resolve(field.DeclaringType);

            if (type == null)
            {
                return(null);
            }

            foreach (var f in type.Fields)
            {
                if (MemberReferenceHelper.compareFieldReference(field, f))
                {
                    return(f);
                }
            }

            return(null);
        }
Пример #2
0
        public MField getField(FieldReference fieldReference)
        {
            var type = getType(fieldReference.DeclaringType);

            if (type != null)
            {
                return(type.getField(fieldReference));
            }
            if (!MemberReferenceHelper.compareTypes(moduleType, fieldReference.DeclaringType))
            {
                return(null);
            }

            initGlobalFields();
            foreach (var field in tokenToGlobalField.Values)
            {
                if (MemberReferenceHelper.compareFieldReference(fieldReference, field.fieldDefinition))
                {
                    return(field);
                }
            }

            return(null);
        }
Пример #3
0
        static MethodReference getVbHandler(MethodDefinition method, out string eventName)
        {
            eventName = null;
            if (method.Body == null)
            {
                return(null);
            }
            if (method.MethodReturnType.ReturnType.FullName != "System.Void")
            {
                return(null);
            }
            if (method.Parameters.Count != 1)
            {
                return(null);
            }
            if (method.Body.Variables.Count != 1)
            {
                return(null);
            }
            if (!isEventHandlerType(method.Body.Variables[0].VariableType))
            {
                return(null);
            }

            var instructions = method.Body.Instructions;
            int index        = 0;

            int newobjIndex = findInstruction(instructions, index, Code.Newobj);

            if (newobjIndex == -1 || findInstruction(instructions, newobjIndex + 1, Code.Newobj) != -1)
            {
                return(null);
            }
            if (!isEventHandlerCtor(instructions[newobjIndex].Operand as MethodReference))
            {
                return(null);
            }
            if (newobjIndex < 1)
            {
                return(null);
            }
            var ldvirtftn = instructions[newobjIndex - 1];

            if (ldvirtftn.OpCode.Code != Code.Ldvirtftn && ldvirtftn.OpCode.Code != Code.Ldftn)
            {
                return(null);
            }
            var handlerMethod = ldvirtftn.Operand as MethodReference;

            if (handlerMethod == null)
            {
                return(null);
            }
            if (!MemberReferenceHelper.compareTypes(method.DeclaringType, handlerMethod.DeclaringType))
            {
                return(null);
            }
            index = newobjIndex;

            FieldReference  addField, removeField;
            MethodReference addMethod, removeMethod;

            if (!findEventCall(instructions, ref index, out removeField, out removeMethod))
            {
                return(null);
            }
            if (!findEventCall(instructions, ref index, out addField, out addMethod))
            {
                return(null);
            }

            if (findInstruction(instructions, index, Code.Callvirt) != -1)
            {
                return(null);
            }
            if (!MemberReferenceHelper.compareFieldReference(addField, removeField))
            {
                return(null);
            }
            if (!MemberReferenceHelper.compareTypes(method.DeclaringType, addField.DeclaringType))
            {
                return(null);
            }
            if (!MemberReferenceHelper.compareTypes(addMethod.DeclaringType, removeMethod.DeclaringType))
            {
                return(null);
            }
            if (!Utils.StartsWith(addMethod.Name, "add_", StringComparison.Ordinal))
            {
                return(null);
            }
            if (!Utils.StartsWith(removeMethod.Name, "remove_", StringComparison.Ordinal))
            {
                return(null);
            }
            eventName = addMethod.Name.Substring(4);
            if (eventName != removeMethod.Name.Substring(7))
            {
                return(null);
            }
            if (eventName == "")
            {
                return(null);
            }

            return(handlerMethod);
        }
Пример #4
0
        bool checkProxyMethod(MethodDefinition method, out DelegateInfo info)
        {
            info = null;
            if (!method.IsStatic || method.Body == null)
            {
                return(false);
            }

            var instrs = method.Body.Instructions;

            if (instrs.Count < 7)
            {
                return(false);
            }

            int index = 0;

            if (instrs[index].OpCode.Code != Code.Ldsfld)
            {
                return(false);
            }
            var field = instrs[index++].Operand as FieldDefinition;

            if (field == null || !field.IsStatic)
            {
                return(false);
            }
            if (!MemberReferenceHelper.compareTypes(method.DeclaringType, field.DeclaringType))
            {
                return(false);
            }

            if (!DotNetUtils.isBrtrue(instrs[index++]))
            {
                return(false);
            }
            if (instrs[index++].OpCode.Code != Code.Ldnull)
            {
                return(false);
            }
            if (instrs[index].OpCode.Code != Code.Ldftn)
            {
                return(false);
            }
            var calledMethod = instrs[index++].Operand as MethodReference;

            if (calledMethod == null)
            {
                return(false);
            }
            if (instrs[index++].OpCode.Code != Code.Newobj)
            {
                return(false);
            }
            if (instrs[index].OpCode.Code != Code.Stsfld)
            {
                return(false);
            }
            if (!MemberReferenceHelper.compareFieldReference(field, instrs[index++].Operand as FieldReference))
            {
                return(false);
            }
            if (instrs[index].OpCode.Code != Code.Ldsfld)
            {
                return(false);
            }
            if (!MemberReferenceHelper.compareFieldReference(field, instrs[index++].Operand as FieldReference))
            {
                return(false);
            }

            for (int i = 0; i < method.Parameters.Count; i++)
            {
                if (index >= instrs.Count)
                {
                    return(false);
                }
                if (DotNetUtils.getArgIndex(instrs[index++]) != i)
                {
                    return(false);
                }
            }

            if (index + 2 > instrs.Count)
            {
                return(false);
            }
            var call = instrs[index++];

            if (call.OpCode.Code != Code.Callvirt)
            {
                return(false);
            }

            if (instrs[index++].OpCode.Code != Code.Ret)
            {
                return(false);
            }

            info = new DelegateInfo(field, calledMethod, OpCodes.Call);
            return(true);
        }