Introduction
In this article, you will learn about the newly introduced Registration Builder Class in .NET 4.5.
This class is introduced through the MEF.
The Registration Builder Class comes with three different types of the For() Method.
1. ForType(): This selects only a single type.
Example
-
- BuildRegistration.ForType<NewUser>().Export<NewUser>();
2. ForTypeDerivedFrom(): This selects those types that are assignable to a Base Class or an interface.
Example
-
- BuildRegistration.ForTypesDerivedFrom(typeof(WishName)).SelectConstructor(cInfo => cInfo[0]).Export<WishName>();
3. ForTypeMatching(): This selects those types that match a Boolean Selector.
Example
-
- BuildRegistration.ForTypesMatching(x => x.Name.Equals("WelcomeToHome")).Export();
I had shown all these examples in my Application whose code is as follows.
Step 1
Add a class for wishing a user a happy birthday.
Add this code in that class:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace MEF2Example
- {
- class Birthday : WishName
- {
- public string Greet(string User)
- {
- return string.Format("Hey Dude It's Your B'day Today:- {0}", User);
- }
- }
- }
Step 2
Add a class that will be used to greet the user daily.
The code is as follows:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.Composition;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace MEF2Example
- {
- public class Morning : WishName
- {
- public string wishes { get; set; }
-
- public string Greet(string User)
- {
- return string.Format("Wake Up Some 1 Is Waiting For You (Your BOSS, hihihihi) {0}", User);
- }
- }
- }
Step 3
Similarly add classes to wish various kind of things and then add a class to pass the name of the user of which all these wishes are to be made:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.Composition;
- using System.ComponentModel.Composition.Hosting;
- using System.ComponentModel.Composition.Registration;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace MEF2Example
- {
- public class StartMFL
- {
- static RegistrationBuilder BuildRegistration = default(RegistrationBuilder);
- static AssemblyCatalog AssamCat = default(AssemblyCatalog);
- static AggregateCatalog AggCat = default(AggregateCatalog);
- static CompositionContainer Compocont = default(CompositionContainer);
-
- public static CompositionContainer Container()
- {
- try
- {
- BuildRegistration = new RegistrationBuilder();
-
-
-
- BuildRegistration.ForType<NewUser>().Export<NewUser>();
-
-
-
- BuildRegistration.ForTypesDerivedFrom(typeof(WishName)).SelectConstructor(cInfo => cInfo[0]).Export<WishName>();
-
-
- BuildRegistration.ForTypesMatching(x => x.Name.Equals("WelcomeToHome")).Export();
-
-
-
- BuildRegistration.ForType<Export>().Export<Export>();
-
- AssamCat = new AssemblyCatalog(Assembly.GetExecutingAssembly(), BuildRegistration);
- AggCat = new AggregateCatalog(AssamCat);
-
- Compocont = new CompositionContainer(AggCat, CompositionOptions.DisableSilentRejection);
- Compocont.ComposeParts();
- }
- catch (System.ComponentModel.Composition.CompositionException ex)
- {
- Console.WriteLine(ex.Message);
- }
-
- return Compocont;
- }
- }
- }
You can download the files provided to see the application running.