Skip to main content

.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
  1. public static void DoSomethingWithThisList(List<SampleClass> tempList)
  2. {
  3.     //Do something:: With parameter
  4.     foreach(var itemList in tempList)
  5.     {
  6.         var getProperty = itemList.SampleProperty;
  7.     }
  8. }

where SampleClass is an a class or struc (below) containing your properties.

Code Snippet
  1. public class SampleClass
  2. {
  3.     public string SampleProperty { get; set; }
  4. }

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
  1. public static void DoDynamics(List<dynamic> tempList)
  2. {
  3.     //Do something:: with anonymous parameter
  4.     //Intellisense is happy -> Properties are evaluated at runtime
  5.     foreach (var itemList in tempList)
  6.     {
  7.         var getProperty = itemList.SampleProperty;
  8.     }
  9. }

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

Popular posts from this blog

Turning WCF Service into .asmx for debugging

Even though .asmx web services are becoming dinosaurs of the new .NET world of WCF. I missed the simplicity of debugging code right in visual studio without: Creating a client to consume WCF service Attaching w3p.exe process and Adding break points  One quick solution: Turn WCF service into .asmx service with few lines of code, debug your code with asmx, and turn .asmx off during deployment.  Detail steps: 1- First take your WCF class and add WebService attribute to it Code Snippet /// <summary> /// Dual mode with .ASMX and WCF /// </summary> [ WebService (Namespace = "http://www.yourdomain.com/" )] 2- Then add WebMethod attribute to a function you want to expose in .asmx Code Snippet [ WebMethod ] public List < PageController . Page > DualFunction() { 3- Take the .svc file from your solution - copy and rename the copied file [YourOriginalWCFFile.asmx]. Open up the copied file and rename "ServiceHost&qu

ASP.MVC Real-time data processing with Sharded RavenDB and SignalR

In a move that some call "Radical” The Weather Channel recently swapped their enterprise Oracle and MySql Databases for NoSQL  MongoDB .  One word that could describe the rapid adoption of NoSQL databases compared to relational once is “ sharding .” Unlike relational databases, NoSQL systems allow developers to be active participants in the data storage process by providing a way to create logic (sharding strategy) that outlines how and where data could be stored, resulting in better scalability and improved performance. RavenDB , an open source NoSQL system for .NET that is easy to deploy (both as a standalone/embedded system) for an ASP.MVC application. SingalR is a tool that one can use in a .NET environment to add real-time client-server processing in an application. SignalR allows a developer to broadcast messages to all (or some) clients from a centralized client and/or server call. I was looking for a quick data-sharding example with embedded RavenDB that I can

Processing ASP MVC Web API Requests in Multi-threaded JS Web Worker

Unlike an asynchronous Ajax call, HTML5 Web workers provide an opportunity to run a Multi-threaded JavaScript code in modern browsers that support them . Each worker spawns an isolated thread with dedicated JavaScript Event Loop, Stack and Heap memory. For example a regular Ajax Call to MVC Web API service call goes through the following asynchronous call cycle. The JavaScript Event Loop in this case could be interrupted by events that are being executed on the UI; for instance, a "window.alert", could possibly stop all scripts on the page from executing until a user responds. Replacing the Ajax Call with HTML5 web worker provides a great way to run long running scripts in separate threads so that asynchronous code execution is not interrupted by UI events. Here is the a JavaScript worker implementation of the same MVC Web API call using a JavaScript web worker. Despite the advantages of using a web worker, implementing one requires working with some constr