.NET, Architecture, ALM, Cloud

Publicité

SwitchExpression Example

C# Code :

Action<AttributeTargets> switchSample = (a) =>
                {
                    switch (a)
                    {
                        case AttributeTargets.Class:
                            Console.WriteLine("Attribute for class");
                            break;
                        case AttributeTargets.Method:
                            Console.WriteLine("Attribute for method");
                            break;
                        default:
                            throw new NotImplementedException();
                    }
                };


Expression Trees Code :

var aParameter = Expression.Parameter(typeof(AttributeTargets), "a");           Expression<Action<AttributeTargets>> expression =  
       Expression.Lambda<Action<AttributeTargets>>(
                //switch (a)
                Expression.Switch(

                    aParameter,
                    //default: throw new NotImplementedException();
                    Expression.Throw(
Expression.New(typeof(NotImplementedException))),

                     //case AttributeTargets.Class:...
                    Expression.SwitchCase(

                        Expression.Call(typeof(Console).GetMethod("WriteLine",
                        new Type[] { typeof(string) }), Expression.Constant("Attribute for class")),
                        Expression.Constant(AttributeTargets.Class)),
                     //case AttributeTargets.Method:...
                     Expression.SwitchCase(

                        Expression.Call(typeof(Console).GetMethod("WriteLine",
                        new Type[] { typeof(string) }), Expression.Constant("Attribute for method")),
                        Expression.Constant(AttributeTargets.Method))),
                        aParameter);

 

Expression Trees Debug View :

.Lambda #Lambda1<System.Action`1[System.AttributeTargets]>(System.AttributeTargets $a) {
    .Switch ($a) {
    .Case (.Constant<System.AttributeTargets>(Class)):
            .Call System.Console.WriteLine("Attribute for class")
    .Case (.Constant<System.AttributeTargets>(Method)):
            .Call System.Console.WriteLine("Attribute for method")
    .Default:
            .Throw .Constant<System.NotImplementedException>(System.NotImplementedException: The method or operation is not implemented.)
    }
}

 

IL Code :

IL_0000: ldarg.1
IL_0001: stloc.0
//case AttributeTargets.Class:
IL_0002: ldloc.0
IL_0003: ldc.i4.4
IL_0004: beq IL_0016
//case AttributeTargets.Method:
IL_0009: ldloc.0
IL_000a: ldc.i4.s 64
IL_000c: beq IL_0025
IL_0011: br IL_0034
//Console.WriteLine("Attribute for class");
IL_0016: ldstr 70000003 "Attribute for class"
IL_001b: call 6000004 System.Console.WriteLine(System.String)
//break;
IL_0020: br IL_0042
//Console.WriteLine("Attribute for method");
IL_0025: ldstr 70000005 "Attribute for method"
IL_002a: call 6000006 System.Console.WriteLine(System.String)
//break;
IL_002f: br IL_0042
//default: throw new NotImplementedException();
IL_0034: newobj 6000007 System.NotImplementedException..ctor()
IL_0039: throw
IL_0042: ret

Publicité
Retour à l'accueil
Partager cet article
Repost0
Pour être informé des derniers articles, inscrivez vous :
Commenter cet article