Skip to content

Instantly share code, notes, and snippets.

@hmemcpy
Created June 4, 2012 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmemcpy/2868064 to your computer and use it in GitHub Desktop.
Save hmemcpy/2868064 to your computer and use it in GitHub Desktop.
Non-generic pattern match
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