/// <summary> /// Determine if the specified <see cref="TypeRefBase"/> refers to the same generic type, regardless of actual type arguments. /// </summary> public override bool IsSameGenericType(TypeRefBase typeRefBase) { TypeRefBase typeRef = Type; return(typeRef != null && typeRef.IsSameGenericType(typeRefBase)); }
/// <summary> /// Get the parent delegate expression. /// </summary> public virtual Expression GetParentDelegateExpression() { // Determine the delegate type to which the anonymous method is being assigned Expression delegateExpression = null; CodeObject parent = _parent; while (parent is Conditional || parent is IfNullThen) { parent = parent.Parent; } if (parent is VariableDecl) { // If the anonymous method is being used to initialize a variable, get the delegate type from the variable's type delegateExpression = ((VariableDecl)parent).Type; } else if (parent is Assignment) { // If the anonymous method is being assigned to a variable, get the delegate type from the variable's type delegateExpression = ((Assignment)parent).Left.SkipPrefixes(); } else if (parent is Cast) { // If the anonymous method is being cast, get the delegate type from the cast delegateExpression = ((Cast)parent).Type; } else if (parent is Return) { // If the anonymous method is being returned from a method, get the delegate type from the method return type CodeObject parentMethod = parent.FindParentMethod(); if (parentMethod is MethodDeclBase) { delegateExpression = ((MethodDeclBase)parentMethod).ReturnType; } else if (parentMethod is AnonymousMethod) { // If the anonymous method is being returned from another anonymous method, recursively get the parent // delegate expression of the parent anonymous method, then get the return type of that delegate. delegateExpression = ((AnonymousMethod)parentMethod).GetParentDelegateExpression(); if (delegateExpression != null) { delegateExpression = delegateExpression.GetDelegateReturnType(); } } } else if (parent is YieldReturn) { // If the anonymous method is being returned from an iterator method using 'yield return', get the delegate // type from the method return type, minus the IEnumerable<> wrapper. CodeObject parentMethod = parent.FindParentMethod(); if (parentMethod is MethodDeclBase) { delegateExpression = ((MethodDeclBase)parentMethod).ReturnType; TypeRefBase typeRefBase = delegateExpression.SkipPrefixes() as TypeRefBase; if (typeRefBase != null && typeRefBase.IsSameGenericType(TypeRef.IEnumerable1Ref)) { delegateExpression = typeRefBase.TypeArguments[0]; } } } else if (parent is Initializer) { // Handle collection initializers if (parent.Parent is Initializer && parent.Parent.Parent is NewObject) { TypeRefBase typeRefBase = ((NewObject)parent.Parent.Parent).SkipPrefixes() as TypeRefBase; // Handle a Dictionary where the value is a delegate type if (typeRefBase != null && typeRefBase.IsSameGenericType(TypeRef.Dictionary2Ref)) { delegateExpression = typeRefBase.TypeArguments[1]; } } } return(delegateExpression); }