本文整理汇总了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;
示例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;
}