Created
June 4, 2012 12:28
-
-
Save hmemcpy/2868064 to your computer and use it in GitHub Desktop.
Non-generic pattern match
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public override IEnumerable<IComponentRegistration> GetComponentRegistrations(ITreeNode registrationRootElement) | |
{ | |
IStructuralMatchResult match = Match(registrationRootElement); | |
if (match.Matched) | |
{ | |
// ReSharper SSR will match a generic method with this pattern too. If has generic arguments, return | |
var invocationExpression = match.MatchedElement as IInvocationExpression; | |
if (invocationExpression == null || invocationExpression.TypeArguments.Count > 0) | |
{ | |
yield break; | |
} | |
var fromTypeArgument = match.GetMatchedElement("fromType") as ICSharpArgument; | |
var toTypeArgument = match.GetMatchedElement("toType") as ICSharpArgument; | |
if (fromTypeArgument == null || toTypeArgument == null) | |
{ | |
yield break; | |
} | |
var fromTypeOf = fromTypeArgument.Value as ITypeofExpression; | |
var toTypeOf = toTypeArgument.Value as ITypeofExpression; | |
if (fromTypeOf == null || toTypeOf == null) | |
{ | |
yield break; | |
} | |
var fromType = fromTypeOf.ArgumentType as IDeclaredType; | |
var toType = toTypeOf.ArgumentType as IDeclaredType; | |
if (fromType == null || toType == null) | |
{ | |
yield break; | |
} | |
var serviceTypeElement = fromType.GetTypeElement(); | |
var typeTypeElement = toType.GetTypeElement(); | |
if (serviceTypeElement == null || typeTypeElement == null) | |
{ | |
yield break; | |
} | |
yield return new ComponentRegistration(registrationRootElement, serviceTypeElement) | |
{ | |
Implementation = typeTypeElement | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment