One of the enhancement that I really like about .NET 4.0 is the ability to work with DynamicExpression class. Pre .NET 4.0 if you want to pass a List to a function you would have to do something like:
where SampleClass is an a class or struc (below) containing your properties.
Fast forward in .NET 4.0 & 4.5 you could easily pass an anonymous object or list of objects and let the compiler evaluate the methods and properties associated with your custom objects on runtime.
To unleash DynamicExpression:
1- Add reference to "Microsoft.CSharp" or "Microsoft.VisualBasic" to your project
2- Start passing Dynamic objects
Example:
Caveat: I have not tested if there are any performance issues introduced as a result of using DynamicExpressions; however, I would assume that there will be some overhead involved as the compiler have to do some reflection before invoking any custom object methods or calling custom object properties.
Code Snippet
- public static void DoSomethingWithThisList(List<SampleClass> tempList)
- {
- //Do something:: With parameter
- foreach(var itemList in tempList)
- {
- var getProperty = itemList.SampleProperty;
- }
- }
where SampleClass is an a class or struc (below) containing your properties.
Code Snippet
- public class SampleClass
- {
- public string SampleProperty { get; set; }
- }
Fast forward in .NET 4.0 & 4.5 you could easily pass an anonymous object or list of objects and let the compiler evaluate the methods and properties associated with your custom objects on runtime.
To unleash DynamicExpression:
1- Add reference to "Microsoft.CSharp" or "Microsoft.VisualBasic" to your project
2- Start passing Dynamic objects
Example:
Code Snippet
- public static void DoDynamics(List<dynamic> tempList)
- {
- //Do something:: with anonymous parameter
- //Intellisense is happy -> Properties are evaluated at runtime
- foreach (var itemList in tempList)
- {
- var getProperty = itemList.SampleProperty;
- }
- }
Caveat: I have not tested if there are any performance issues introduced as a result of using DynamicExpressions; however, I would assume that there will be some overhead involved as the compiler have to do some reflection before invoking any custom object methods or calling custom object properties.
Comments