Skip to content

Instantly share code, notes, and snippets.

@riezebosch riezebosch/switch.cs
Last active Oct 4, 2019

Embed
What would you like to do?
exploring C# 8
public static IArgumentMatcher? Create(Expression arg) => arg switch
{
MethodCallExpression call when call.Method.DeclaringType == typeof(Arg) => call.Method.Name switch
{
nameof(Arg.Ignore) => (IArgumentMatcher)new IgnoreArgument(),
nameof(Arg.Where) => new LambdaArgument(call.Arguments.Single()),
_ => throw new InvalidOperationException()
},
_ => null
};
public static IArgumentMatcher Create(Expression arg)
{
if (arg is MethodCallExpression call && call.Method.DeclaringType == typeof(Arg))
{
switch (call.Method.Name)
{
case "Ignore":
return new IgnoreArgument();
case "Equals":
return new EqualsArgument(call.Arguments[0]);
default:
throw new NotImplementedException();
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.