Exemplo n.º 1
0
        TypeResolver getTypeResolver(TypeReference typeReference)
        {
            var key = typeReference.Namespace + "." + typeReference.Name;
            List <TypeResolver> list;

            if (!types.TryGetValue(key, out list))
            {
                return(null);
            }

            if (typeReference is TypeDefinition)
            {
                foreach (var resolver in list)
                {
                    if (resolver.type.MetadataToken == typeReference.MetadataToken.ToInt32())
                    {
                        return(resolver);
                    }
                }
            }

            foreach (var resolver in list)
            {
                if (ResolverUtils.compareTypes(resolver.type, typeReference))
                {
                    return(resolver);
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        public MethodBase resolve(MethodReference methodReference)
        {
            initMethods();

            List <MethodBase> list;

            if (!methods.TryGetValue(methodReference.Name, out list))
            {
                return(null);
            }

            var git = methodReference.DeclaringType as GenericInstanceType;
            var gim = methodReference as GenericInstanceMethod;

            methodReference = MethodReferenceInstance.make(methodReference, git, gim);

            foreach (var method in list)
            {
                if (ResolverUtils.compareMethods(method, methodReference))
                {
                    return(method);
                }
            }

            return(null);
        }
Exemplo n.º 3
0
 public void SetMethodInfo(MMethod methodInfo)
 {
     this.methodInfo  = methodInfo;
     methodReturnType = ResolverUtils.GetReturnType(methodInfo.methodBase);
     methodParameters = GetMethodParameterTypes(methodInfo.methodBase);
     delegateType     = Utils.GetDelegateType(methodReturnType, methodParameters);
 }
Exemplo n.º 4
0
        TypeResolver GetTypeResolver(ITypeDefOrRef typeRef)
        {
            if (typeRef == null)
            {
                return(null);
            }
            var scopeType = typeRef.ScopeType;
            var key       = scopeType.Namespace + "." + scopeType.TypeName;

            if (!types.TryGetValue(key, out var list))
            {
                return(null);
            }

            if (scopeType is TypeDef)
            {
                foreach (var resolver in list)
                {
                    if (resolver.type.MetadataToken == scopeType.MDToken.Raw)
                    {
                        return(resolver);
                    }
                }
            }

            foreach (var resolver in list)
            {
                if (ResolverUtils.CompareTypes(resolver.type, scopeType))
                {
                    return(resolver);
                }
            }

            return(null);
        }
Exemplo n.º 5
0
        public FieldInfo resolve(FieldReference fieldReference)
        {
            initFields();

            List <FieldInfo> list;

            if (!fields.TryGetValue(fieldReference.Name, out list))
            {
                return(null);
            }

            var git = fieldReference.DeclaringType as GenericInstanceType;

            if (git != null)
            {
                fieldReference = FieldReferenceInstance.make(fieldReference, git);
            }

            foreach (var field in list)
            {
                if (ResolverUtils.compareFields(field, fieldReference))
                {
                    return(field);
                }
            }

            return(null);
        }
Exemplo n.º 6
0
 FieldInfo resolveGlobalField(FieldReference fieldReference)
 {
     initGlobalFields();
     foreach (var globalField in globalFields)
     {
         if (ResolverUtils.compareFields(globalField, fieldReference))
         {
             return(globalField);
         }
     }
     return(null);
 }
Exemplo n.º 7
0
 MethodBase ResolveGlobalMethod(IMethod methodRef)
 {
     InitGlobalMethods();
     foreach (var globalMethod in globalMethods)
     {
         if (ResolverUtils.CompareMethods(globalMethod, methodRef))
         {
             return(globalMethod);
         }
     }
     return(null);
 }
Exemplo n.º 8
0
 MethodBase resolveGlobalMethod(MethodReference methodReference)
 {
     initGlobalMethods();
     foreach (var globalMethod in globalMethods)
     {
         if (ResolverUtils.compareMethods(globalMethod, methodReference))
         {
             return(globalMethod);
         }
     }
     return(null);
 }
Exemplo n.º 9
0
 FieldInfo ResolveGlobalField(IField fieldRef)
 {
     InitGlobalFields();
     foreach (var globalField in globalFields)
     {
         if (ResolverUtils.CompareFields(globalField, fieldRef))
         {
             return(globalField);
         }
     }
     return(null);
 }
Exemplo n.º 10
0
        public RewrittenMethod createDelegate(MethodBase realMethod)
        {
            var newMethodInfo = realMethodToNewMethod[realMethod];

            if (newMethodInfo.rewrittenMethod != null)
            {
                return(newMethodInfo.rewrittenMethod);
            }

            var dm  = new DynamicMethod(newMethodInfo.rewrittenMethodName, typeof(object), new Type[] { GetType(), typeof(object[]) }, newMethodInfo.oldMethod.Module, true);
            var ilg = dm.GetILGenerator();

            ilg.Emit(ROpCodes.Ldarg_0);
            ilg.Emit(ROpCodes.Ldc_I4, newMethodInfo.delegateIndex);
            ilg.Emit(ROpCodes.Call, GetType().GetMethod("rtGetDelegateInstance", BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Instance));
            ilg.Emit(ROpCodes.Castclass, newMethodInfo.delegateType);

            var args = newMethodInfo.oldMethod.GetParameters();

            for (int i = 0; i < args.Length; i++)
            {
                var arg = args[i].ParameterType;

                ilg.Emit(ROpCodes.Ldarg_1);
                ilg.Emit(ROpCodes.Ldc_I4, i);
                ilg.Emit(ROpCodes.Ldelem_Ref);

                if (arg.IsValueType)
                {
                    ilg.Emit(ROpCodes.Unbox_Any, arg);
                }
                else
                {
                    ilg.Emit(ROpCodes.Castclass, arg);
                }
            }
            ilg.Emit(ROpCodes.Ldarg_0);

            var flags        = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance;
            var invokeMethod = newMethodInfo.delegateType.GetMethod("Invoke", flags);

            ilg.Emit(ROpCodes.Call, invokeMethod);
            if (ResolverUtils.getReturnType(newMethodInfo.oldMethod) == typeof(void))
            {
                ilg.Emit(ROpCodes.Ldnull);
            }
            ilg.Emit(ROpCodes.Ret);

            newMethodInfo.rewrittenMethod = (RewrittenMethod)dm.CreateDelegate(typeof(RewrittenMethod), this);
            return(newMethodInfo.rewrittenMethod);
        }
Exemplo n.º 11
0
        Type[] GetMethodParameterTypes(MethodBase method)
        {
            var list = new List <Type>();

            if (ResolverUtils.HasThis(method))
            {
                list.Add(method.DeclaringType);
            }

            foreach (var param in method.GetParameters())
            {
                list.Add(param.ParameterType);
            }

            thisArgIndex = list.Count;
            list.Add(methodsRewriter.GetType());

            return(list.ToArray());
        }
Exemplo n.º 12
0
        void InitMethods()
        {
            if (methods != null)
            {
                return;
            }
            methods = new Dictionary <string, List <MethodBase> >(StringComparer.Ordinal);

            var flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;

            foreach (var method in ResolverUtils.GetMethodBases(type, flags))
            {
                if (!methods.TryGetValue(method.Name, out var list))
                {
                    methods[method.Name] = list = new List <MethodBase>();
                }
                list.Add(method);
            }
        }
Exemplo n.º 13
0
        public FieldInfo Resolve(IField fieldRef)
        {
            InitFields();

            if (!fields.TryGetValue(fieldRef.Name.String, out var list))
            {
                return(null);
            }

            fieldRef = GenericArgsSubstitutor.Create(fieldRef, fieldRef.DeclaringType.TryGetGenericInstSig());

            foreach (var field in list)
            {
                if (ResolverUtils.CompareFields(field, fieldRef))
                {
                    return(field);
                }
            }

            return(null);
        }
Exemplo n.º 14
0
        public MethodBase Resolve(IMethod methodRef)
        {
            InitMethods();

            if (!methods.TryGetValue(methodRef.Name.String, out var list))
            {
                return(null);
            }

            methodRef = GenericArgsSubstitutor.Create(methodRef, methodRef.DeclaringType.TryGetGenericInstSig());

            foreach (var method in list)
            {
                if (ResolverUtils.CompareMethods(method, methodRef))
                {
                    return(method);
                }
            }

            return(null);
        }
Exemplo n.º 15
0
        void InitMethods()
        {
            if (tokenToMethod != null)
            {
                return;
            }
            tokenToMethod     = new Dictionary <int, MMethod>(typeDef.Methods.Count);
            methodRefToMethod = new MethodDefDict <MMethod>();

            var tmpTokenToMethod = new Dictionary <int, MethodBase>();
            var flags            = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

            foreach (var m in ResolverUtils.GetMethodBases(type, flags))
            {
                tmpTokenToMethod[m.MetadataToken] = m;
            }
            foreach (var m in typeDef.Methods)
            {
                var token  = (int)m.MDToken.Raw;
                var method = new MMethod(tmpTokenToMethod[token], m);
                tokenToMethod[token] = method;
                methodRefToMethod.Add(method.methodDef, method);
            }
        }
Exemplo n.º 16
0
 public TypeInstanceResolver(Type type, ITypeDefOrRef typeRef)
 {
     this.type = ResolverUtils.MakeInstanceType(type, typeRef);
 }
Exemplo n.º 17
0
        static FieldInfo getStackFrameMethodField()
        {
            var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

            return(ResolverUtils.getFieldThrow(typeof(StackFrame), typeof(MethodBase), flags, "Could not find StackFrame's method (MethodBase) field"));
        }
Exemplo n.º 18
0
        static FieldInfo getStackTraceStackFramesField()
        {
            var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

            return(ResolverUtils.getFieldThrow(typeof(StackTrace), typeof(StackFrame[]), flags, "Could not find StackTrace's frames (StackFrame[]) field"));
        }
Exemplo n.º 19
0
 public TypeInstanceResolver(Type type, TypeReference typeReference)
 {
     this.type = ResolverUtils.makeInstanceType(type, typeReference);
 }