當前位置: 首頁>>代碼示例>>C#>>正文


C# MethodReference.ChangeDeclaringType方法代碼示例

本文整理匯總了C#中Mono.Cecil.MethodReference.ChangeDeclaringType方法的典型用法代碼示例。如果您正苦於以下問題:C# MethodReference.ChangeDeclaringType方法的具體用法?C# MethodReference.ChangeDeclaringType怎麽用?C# MethodReference.ChangeDeclaringType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Mono.Cecil.MethodReference的用法示例。


在下文中一共展示了MethodReference.ChangeDeclaringType方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: CreateFunction

        /// <summary>
        /// Creates the function.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <returns></returns>
        Function CreateFunction(MethodReference method)
        {
            Function function;
            if (functions.TryGetValue(method, out function))
                return function;

            var resolvedMethod = method.Resolve();
            var declaringType = GetType(ResolveGenericsVisitor.Process(method, method.DeclaringType), TypeState.Opaque);

            // Check if method is only defined in a parent class (can happen in some rare case, i.e. PCL TypeInfo.get_Assembly()).
            bool hasMatch = MetadataResolver.GetMethod(declaringType.TypeDefinitionCecil.Methods, method.GetElementMethod()) != null;
            if (resolvedMethod != null && !hasMatch)
            {
                var parentType = declaringType.TypeDefinitionCecil.BaseType != null ? ResolveGenericsVisitor.Process(declaringType.TypeReferenceCecil, declaringType.TypeDefinitionCecil.BaseType) : null;
                if (parentType == null)
                    throw new InvalidOperationException(string.Format("Could not find a matching method in any of the type or its parent for {0}", method));

                // Create function with parent type
                // TODO: Maybe we need to replace generic context with parent type?
                var parentMethod = method.ChangeDeclaringType(parentType);
                function = CreateFunction(parentMethod);

                // Register it so that it can be cached
                functions.Add(method, function);
                return function;
            }

            var returnType = GetType(ResolveGenericsVisitor.Process(method, method.ReturnType), TypeState.StackComplete);
            var parameterTypesBuilder = new List<Type>();
            if (method.HasThis)
            {
                var parameterType = declaringType.TypeReferenceCecil;

                // Value type uses ByReference type for this
                if (declaringType.TypeDefinitionCecil.IsValueType)
                    parameterType = parameterType.MakeByReferenceType();

                parameterTypesBuilder.Add(GetType(parameterType, TypeState.StackComplete));
            }
            foreach (var parameter in method.Parameters)
            {
                parameterTypesBuilder.Add(GetType(ResolveGenericsVisitor.Process(method, parameter.ParameterType), TypeState.StackComplete));
            }

            var parameterTypes = parameterTypesBuilder.ToArray();

            // Find calling convention
            var callingConvention = method.CallingConvention;
            PInvokeInfo pinvokeInfo = null;
            if (resolvedMethod != null && resolvedMethod.HasPInvokeInfo)
            {
                pinvokeInfo = resolvedMethod.PInvokeInfo;
                if (resolvedMethod.PInvokeInfo.IsCallConvStdCall || resolvedMethod.PInvokeInfo.IsCallConvWinapi)
                    callingConvention = MethodCallingConvention.StdCall;
                else if (resolvedMethod.PInvokeInfo.IsCallConvFastcall)
                    callingConvention = MethodCallingConvention.FastCall;
            }

            var functionSignature = new FunctionSignature(abi, returnType, parameterTypes, callingConvention, pinvokeInfo);
            var functionType = CreateFunctionTypeLLVM(functionSignature);

            // If we have an external with generic parameters, let's try to do some generic sharing (we can write only one in C++)
            bool isInternal = resolvedMethod != null && ((resolvedMethod.ImplAttributes & MethodImplAttributes.InternalCall) != 0);
            if (isInternal && resolvedMethod.HasGenericParameters && resolvedMethod.GenericParameters.All(x => x.HasReferenceTypeConstraint))
            {
                // Check if this isn't the shareable method (in which case we should do normal processing)
                if (!((GenericInstanceMethod)method).GenericArguments.All(x => MemberEqualityComparer.Default.Equals(x, @object.TypeReferenceCecil)))
                {
                    // Let's share it with default method
                    var sharedGenericInstance = new GenericInstanceMethod(resolvedMethod);
                    foreach (var genericParameter in resolvedMethod.GenericParameters)
                    {
                        sharedGenericInstance.GenericArguments.Add(@object.TypeReferenceCecil);
                    }

                    var sharedMethod = GetFunction(sharedGenericInstance);

                    // Cast shared function to appropriate pointer type
                    var sharedFunctionGlobal = LLVM.ConstPointerCast(sharedMethod.GeneratedValue, LLVM.PointerType(functionType, 0));
                    function = new Function(declaringType, method, functionType, sharedFunctionGlobal, functionSignature);
                    functions.Add(method, function);

                    return function;
                }
            }

            // Determine if type and function is local, and linkage type
            bool isLocal;
            var linkageType = GetLinkageType(method.DeclaringType, out isLocal);
            if (isInternal)
            {
                // Should be switched to non-weak when we have complete implementation of every internal calls
                linkageType = Linkage.ExternalWeakLinkage;
            }
            else if (resolvedMethod != null && resolvedMethod.HasGenericParameters)
//.........這裏部分代碼省略.........
開發者ID:RainsSoft,項目名稱:SharpLang,代碼行數:101,代碼來源:Compiler.Function.cs


注:本文中的Mono.Cecil.MethodReference.ChangeDeclaringType方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。