.net Frameowrk 4.0 features Chapter 1 :MEF

Managed Extensibility Framework or MEF 

This article is mainly about doing some fun stuff by scratching the surface of:
  • The System.Dynamic namespace introduced in .NET 4.0
  • The System.CompositionModel namespace (Managed Extensibility Framework or MEF - http://mef.codeplex.com/)

A brief note on C# 4.0 dynamic features

C# 4.0 introduced the dynamic keyword to support dynamic typing. If you assign an object to a dynamic type variable (like dynamic myvar=new MyObj()), all method calls, property invocations, and operator invocations on myvar will be delayed till runtime, and the compiler won't perform any type checks for myvar at compile time. So, if you do something like myvar.SomethingStupid();, it is valid at compile time, but invalid at runtime if the object you assigned to myvar doesn't have a SomethingStupid() method.
The System.Dynamic namespace has various classes for supporting dynamic programming, mainly the DynamicObject class from which you can derive your own classes to do runtime dispatching yourself.
You may also want to Read more on Duck Typing, and read a bit on the dynamic keyword and the DLR (Dynamic language runtime).
Back to this article.
The key objective of this article is to demonstrate the multi dimensional possibilities and exciting things we can experience with the dynamic capabilities of C# and .NET 4.0. My intention behind writing this article is to enable it as a learning material for implementing C# dynamic features - and the code attached is for reference regarding how the technique is implemented, rather than what it does (i.e., the functionality).
We’ll be having a couple of interesting objectives:
  • Create a dynamic wrapper around the file system so that we can access files and directories as properties/members of a dynamic object.
  • A way to attach custom methods and operators to our dynamic wrapper class and dispatch them to a plug-in sub system.
My intention behind writing this article is to enable it as a learning material for implementing C# dynamic features - and the code attached is for reference regarding how the technique is implemented. Hence, these objectives are from a learning perspective - and there may not be any significant advantage in accessing the file system via dynamic typing.
You may need to install VS2010 and .NET 4.0 beta to have a look into the code. Click here.

What We'll Achieve

Simply speaking, in the end of the day, here are a couple of things we’ll be able to do.
  • Initialize a dynamically wrapped drive:
  • dynamic CDrive = new FileSystemStorageObject(@"c:\\");
  • Create a sub directory named TestSub:
  • CDrive.CreateSubdirectory("TestSub");
  • Magic - Create a file named File1.txt in the TestSub folder we just created:
  • using (var writer = CDrive.TestSub.File1.txt.CreateText())
    {
        writer.WriteLine("some text in file1.txt");
    }
  • Magic – Wrapping properties with Get/Set methods. E.g., invoking the CreationTime property.
  • Console.WriteLine(CDrive.TestSub.File1.txt.GetCreationTime());
  • More magic - Copy File1.txt to File2.txt using the >> operator:
  • var result = (CDrive.TestSub.File1.txt >> CDrive.TestSub.File2.txt);
  • More magic - Another way of copying, but calling a method in FileInfo using our dynamic type as the parameter
  • CDrive.TestSub.File2.txt.CopyTo(CDrive.TestSub.File3.txt); 
  • Delete the newly created folder:
  • CDrive.TestSub.Delete(true);

Comments