Skip to main content

Posts

Showing posts from March, 2013

.NET 4.0 Evaluating Properties and Methods at Runtime

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: 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- Star