Managed Extensibility Framework or MEF
This article is mainly about doing some fun stuff by scratching the surface of:
The
You may also want to Read more on Duck Typing, and read a bit on the
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:
You may need to install VS2010 and .NET 4.0 beta to have a look into the code. Click here.
Create a sub directory named TestSub:
Magic - Create a file named File1.txt in the TestSub folder we just created:
Magic – Wrapping properties with Get/Set methods. E.g., invoking the
More magic - Copy File1.txt to File2.txt using the
More magic - Another way of copying, but calling a method in
Delete the newly created folder:
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 thedynamic
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.
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:\\");
CDrive.CreateSubdirectory("TestSub");
using (var writer = CDrive.TestSub.File1.txt.CreateText())
{
writer.WriteLine("some text in file1.txt");
}
CreationTime
property.Console.WriteLine(CDrive.TestSub.File1.txt.GetCreationTime());
>>
operator:var result = (CDrive.TestSub.File1.txt >> CDrive.TestSub.File2.txt);
FileInfo
using our dynamic type as the parameterCDrive.TestSub.File2.txt.CopyTo(CDrive.TestSub.File3.txt);
CDrive.TestSub.Delete(true);
Comments
Post a Comment