ExpandoObject Syntax that Compile

141

ExpandoObject Syntax that Compile -

//The code that will compile will look like this:
dynamic cust = new ExpandoObject();
cust.FullName = "Peter Vogel";

// lambda expression doesn't return a value but does accept a single parameter of type string,
// expression needs to be cast as an Action<string> type, like this:
// Note: Inside the lambda expression, if you want to work with a property on the local variable (as I do here), 
// you must refer to the variable that's holding your method. 
// Omitting that reference or using the this keyword won't work.
// Either way, you'll end up referring to the class that this code is inside of, 
// not to the ExpandoObject that the lambda expression is being added to.
cust.ChangeName = (Action<string>) ( (string newName) =>
            {
                cust.FullName = newName;
            } );

// Here, for example, is the code to call my ChangeName method:
cust.ChangeName("Jan Vogel");

// This variation on my ChangeName method accepts a string parameter and returns an integer value, 
// so its Func declaration is Func<string, int>:
cust.ChangeName = (Func<string, int>) ( (string newName) =>
            {
                cust.FullName = newName;
                return cust.FullName.Length;
            } );

            

Comments

Submit
0 Comments