Archive for the ‘C#’ Category

MEF–Managed Extensibility Framework

February 16, 2011

A work colleague and I sat in on a live meeting of Chandan Banerjee from Microsoft talk about MEF.

Mainly work off the “O” in the S.O.L.I.D principle that “software entities … should be open for extension, but closed for modification”.

MEF provides the ability at runtime to resolve class members \ retrieve instances of objects at runtime via attributes.

There is a Host that will consume annotated with the [Import] attribute. In a typical implementation there will be a class library that provides the class to be consumed via the [Export] attribute and maybe a library containing the interface that the consumable classes derive off.

The Host (unfortunately) needs to have some code to load up the catalogue and containers and register itself only after that do the import and exports get matched up.

A catalogue can be a type catalogue, an assembly catalogue, a directory catalogue or a combination of the three. A catalogue is made up of one or more containers, where a container is made up of one or more parts. A part is a class that is marked with the [Export] attribute and matched with a member in the Host application that is marked with the [Import] attribute.

So in the sample below the part is the EmailSender. The Host is consuming the IMessageSender Interface declared in the same assembly the consumption is done via the [Import] on the MessageSender member variable of the Program class. The code to load up the catalogue is in the Compose method of the program class. The compose method loads up the currently executing assembly creates a container based on that then registers itself. The magic occurs in the call to ComposeParts where the Exports and Imports are matched up base on interface name.

The example is a scenario where the Host, the Interface and the part are all in the same assembly. You can just as easily put the host, Interface declaration and Part in their own assembies. If this were the case then the compose method would have to be modified to load the appropriate parts assembly.

   1: using System.ComponentModel.Composition;

   2: using System.ComponentModel.Composition.Hosting;

   3: using System.Reflection;

   4: using System;

   5:  

   6: public class Program

   7: {

   8:   [Import]

   9:   public IMessageSender MessageSender { get; set; }

  10:  

  11:   public static void Main(string[] args)

  12:   {

  13:     Program p = new Program();

  14:     p.Run();

  15:   }

  16:  

  17:   public void Run()

  18:   {

  19:     Compose();

  20:     MessageSender.Send("Message Sent");

  21:   }

  22:  

  23:   private void Compose()

  24:   {

  25:     AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

  26:     var container = new CompositionContainer(catalog);

  27:     container.ComposeParts(this);

  28:   }

  29: }

  30:  

  31: public interface IMessageSender

  32: {

  33:   void Send(string message);

  34: }

  35:  

  36: [Export(typeof(IMessageSender))]

  37: public class EmailSender : IMessageSender

  38: {

  39:   public void Send(string message)

  40:   {

  41:     Console.WriteLine(message);

  42:   }

  43: }

Silverlight 101

July 8, 2009

I finally went to Scott Guthrie’s weblog and attempted his build a Digg Search Client. I got so far as step 5 and I started getting an error while downloading the images from digg.com.

AG_E_NETWORK_ERROR

It was all going sweet till step 5. I have decided not to work around this issue – can anyone solve this for me? I am using Silverlight 3.0 Beta with Visual Studio 2008 SP1. If I solve this I will update this blog with the answer and Scott’s Blog also…

Help…

Asynchronous Programming Using Delegates

April 19, 2009

Here is the most basic use of invocation of a delegate.

   1: using System;

   2: using System.Threading;

   3:  

   4: namespace Delegates1   

   5: {   

   6:     class Program   

   7:     {   

   8:         delegate int sleepTime( int time );

   9:         public int sleepTimeImpl(int time)

  10:         {

  11:             Console.WriteLine("Enter Method sleepTimeImpl. [" + Thread.CurrentThread.ManagedThreadId + "]");

  12:             Thread.Sleep(time);

  13:             Console.WriteLine("Exiting Method sleepTimeImpl. [" + Thread.CurrentThread.ManagedThreadId + "]");

  14:             return time / 1000;

  15:         }

  16:         static void Main(string[] args)

  17:         {

  18:             Program p = new Program();

  19:             sleepTime st = p.sleepTimeImpl;

  20:             Console.WriteLine("Program took {0} seconds.", st.Invoke(1000));

  21:             Console.ReadLine();

  22:         }

  23:     }

  24: }

Next you can call BeginInvoke and EndInvoke to call the method asynchronously.

   1: using System;

   2: using System.Threading;

   3:  

   4: namespace Delegates1

   5: {

   6:     class Program

   7:     {

   8:         delegate int sleepTime( int time );

   9:         public int sleepTimeImpl(int time)

  10:         {

  11:             Console.WriteLine("Enter Method sleepTimeImpl. [" + Thread.CurrentThread.ManagedThreadId + "]");

  12:             Thread.Sleep(time);

  13:             Console.WriteLine("Exiting Method sleepTimeImpl. [" + Thread.CurrentThread.ManagedThreadId + "]");

  14:             return time / 1000;

  15:         }

  16:         static void Main(string[] args)

  17:         {

  18:             Program p = new Program();

  19:             sleepTime st = p.sleepTimeImpl;

  20:             IAsyncResult result = st.BeginInvoke(3000, null, null);

  21:             Console.WriteLine("Called Method.");

  22:             while (!result.IsCompleted)

  23:             {

  24:                 Console.WriteLine("Waiting.");

  25:                 result.AsyncWaitHandle.WaitOne(500, false);

  26:             }

  27:             Console.WriteLine("Program took {0} seconds.", st.EndInvoke(result));

  28:             Console.ReadLine();

  29:         }

  30:     }

  31: }

But to do several task at the same time and use a delegate asynchronously do the following…

   1: using System;

   2: using System.Threading;

   3:  

   4: namespace Delegates1

   5: {

   6:     class Program

   7:     {

   8:         delegate int sleepTime( int time );

   9:         public int sleepTimeImpl(int time)

  10:         {

  11:             Console.WriteLine("Enter Method sleepTimeImpl. [" + Thread.CurrentThread.ManagedThreadId + "]");

  12:             Thread.Sleep(time);

  13:             Console.WriteLine("Exiting Method sleepTimeImpl. [" + Thread.CurrentThread.ManagedThreadId + "]");

  14:             return time / 1000;

  15:         }

  16:         static void Main(string[] args)

  17:         {

  18:             const int arraySize = 6;

  19:             int[] sleepArray = new int[arraySize] { 10000, 9000, 12000, 18000, 16000, 6000 };

  20:             IAsyncResult[] iarArray = new IAsyncResult[arraySize];

  21:             Program program = new Program();

  22:             sleepTime st = program.sleepTimeImpl;

  23:             int i = 0;

  24:             foreach (int sleepItem in sleepArray)

  25:             {

  26:                 iarArray[i++] = st.BeginInvoke(sleepItem, null, null);

  27:             }

  28:             int counter = 0;

  29:             while (counter != arraySize)

  30:             {

  31:                 int current = 0;

  32:                 foreach (IAsyncResult iar in iarArray)

  33:                 {

  34:                     if (iar != null && iar.IsCompleted)

  35:                     {

  36:                         Console.WriteLine("Program took {0} seconds.", st.EndInvoke(iar));

  37:                         iarArray[current] = null;

  38:                         counter++;

  39:                     }

  40:                     current++;

  41:                 }

  42:             }

  43:             Console.ReadLine();

  44:         }

  45:     }

  46: }

 

Visual Studio 2010 and C# 4.0

April 14, 2009

So I went looking for a version of C# 4.0 to download and play with and i came across this site…Visual Studio 2010 and .NET Framework 4.0 CTP

Unfortunately you need to download ~7.5 gig of virtual machine that has Visual Studio 2010 on it and it expires on 1 January 2009. The web site mentions a blog posting that show you how to work around it…Maybe I can be bothered later – but right now I will have to wait till we get it at work.

The Future of C# (Version 4.0)

April 9, 2009

I found a video from Anders Hejlsberg he heads up the language development of C# at Microsoft. There is some interesting stuff on the way with the dynamic features of the language.

There is a new data type called dynamic which is cool. There is better COM interoperability and the compiler is being opened up as a service. With the compiler being exposed you can execute and “compile” code at run-time. There is optional parameters and named parameters. And much more…

Channel9

It goes for about an hour but he (Anders Hejlsberg) is a great presenter and very charismatic.