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


C# TypeNode.GetTemplateInstance方法代碼示例

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


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

示例1: VisitTypeReference


//.........這裏部分代碼省略.........
                    tuExpr.Domains = this.VisitFieldList(tuExpr.Domains);
                    return tuExpr;
                case NodeType.TypeExpression:
                    {
                        TypeExpression tExpr = (TypeExpression)type;
                        tExpr.Expression = this.VisitTypeExpression(tExpr.Expression);
                        if (tExpr.Expression is Literal) return type;
                        tExpr.TemplateArguments = this.VisitTypeReferenceList(tExpr.TemplateArguments);
                        return tExpr;
                    }
                case NodeType.TypeIntersectionExpression:
                    TypeIntersectionExpression tiExpr = (TypeIntersectionExpression)type;
                    tiExpr.Types = this.VisitTypeReferenceList(tiExpr.Types);
                    return tiExpr;
                case NodeType.TypeUnionExpression:
                    TypeUnionExpression tyuExpr = (TypeUnionExpression)type;
                    tyuExpr.Types = this.VisitTypeReferenceList(tyuExpr.Types);
                    return tyuExpr;

                default:
                    TypeNode declaringType = this.VisitTypeReference(type.DeclaringType);
                    if (declaringType != null)
                    {
                        Identifier tname = type.Name;
                        if (type.Template != null && type.IsGeneric) tname = type.Template.Name;
                        TypeNode nt = declaringType.GetNestedType(tname);
                        if (nt != null)
                        {
                            TypeNodeList arguments = type.TemplateArguments;
                            type = nt;
                            if (TargetPlatform.UseGenerics)
                            {
                                if (arguments != null && arguments.Count > 0 && nt.ConsolidatedTemplateParameters != null && nt.ConsolidatedTemplateParameters.Count > 0)
                                    type = nt.GetTemplateInstance(this.TargetModule, this.CurrentType, declaringType, arguments);
                            }
                        }
                    }

                    if(type.Template != null && (type.ConsolidatedTemplateParameters == null || type.ConsolidatedTemplateParameters.Count == 0))
                    {
                        if(!type.IsNotFullySpecialized && (!type.IsNormalized ||
                         (this.CurrentType != null && type.DeclaringModule == this.CurrentType.DeclaringModule)))
                        {
                            return type;
                        }

                        // Type is a template instance, but some of its arguments were themselves parameters.
                        // See if any of these parameters are to be specialized by this specializer.
                        bool mustSpecializeFurther = false;
                        TypeNodeList targs = type.TemplateArguments;
                        int numArgs = targs == null ? 0 : targs.Count;

                        if(targs != null)
                        {
                            targs = new TypeNodeList(targs);

                            for(int i = 0; i < numArgs; i++)
                            {
                                TypeNode targ = targs[i];
                                ITypeParameter tparg = targ as ITypeParameter;

                                if(tparg != null)
                                {
                                    for(int j = 0, np = pars == null ? 0 : pars.Count, m = args == null ? 0 : args.Count; j < np && j < m; j++)
                                    {
                                        //^ assert pars != null && args != null;
開發者ID:julianhaslinger,項目名稱:SHFB,代碼行數:67,代碼來源:Specializer.cs

示例2: GetContinueWithMethod

        /// <summary>
        /// Returns correct version of the ContinueWith method.
        /// </summary>
        /// <remarks>
        /// This function returns ContinueWith overload that takes TaskContinuationOptions.
        /// </remarks>
        private static Method GetContinueWithMethod(Class closureClass, TypeNode taskTemplate, TypeNode taskType)
        {
            var continueWithCandidates = taskTemplate.GetMembersNamed(Identifier.For("ContinueWith"));
            
            // Looking for an overload with TaskContinuationOptions
            const int expectedNumberOfArguments = 2;

            for (int i = 0; i < continueWithCandidates.Count; i++)
            {
                var cand = continueWithCandidates[i] as Method;
                if (cand == null) continue;

                // For non-generic version we're looking for ContinueWith(Action<Task>, TaskContinuationOptions)

                if (!taskType.IsGeneric)
                {
                    if (cand.IsGeneric) continue;

                    if (cand.ParameterCount != expectedNumberOfArguments) continue;

                    if (cand.Parameters[0].Type.GetMetadataName() != "Action`1") continue;
                    
                    if (cand.Parameters[1].Type.GetMetadataName() != "TaskContinuationOptions") continue;

                    return cand;
                }

                // For generic version we're looking for ContinueWith(Func<Task, T>, TaskContinuationOptions)
                if (!cand.IsGeneric) continue;

                if (cand.TemplateParameters.Count != 1) continue;

                if (cand.ParameterCount != expectedNumberOfArguments) continue;

                if (cand.Parameters[0].Type.GetMetadataName() != "Func`2") continue;
                if (cand.Parameters[1].Type.GetMetadataName() != "TaskContinuationOptions") continue;

                // now create instance, first of task
                var taskInstance = taskTemplate.GetTemplateInstance(
                    closureClass.DeclaringModule,
                    taskType.TemplateArguments[0]);

                // ST: some black magic is happening, but it seems it is required to get ContinueWith
                // from generic instantiated version of the task
                var candMethod = (Method)taskInstance.GetMembersNamed(Identifier.For("ContinueWith"))[i];

                // Candidate method would have following signature:
                // Task<T> ContinueWith(Task<T> t) for generic version
                return candMethod.GetTemplateInstance(null, taskType);
            }

            return null;
        }
開發者ID:flcdrg,項目名稱:CodeContracts,代碼行數:59,代碼來源:EmitAsyncClosure.cs


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