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
- ///1: This Will Show You Export type. This Will Export Only One Type.
- BuildRegistration.ForType<NewUser>().Export<NewUser>();
2. ForTypeDerivedFrom(): This selects those types that are assignable to a Base Class or an interface.
Example
- ///2: This Will Show You How To Export all types.
- BuildRegistration.ForTypesDerivedFrom(typeof(WishName)).SelectConstructor(cInfo => cInfo[0]).Export<WishName>();
3. ForTypeMatching(): This selects those types that match a Boolean Selector.
Example
- ///3: This Will Show You How To Export types matching
- 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);
- }
- }
- }

subash jayanPosted Aug 26, 2015, 6:38 PM
Good one.