CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Overriding InheritedExport sample

1018 Views
Copy Code Show/Hide Line Numbers
/*

Sample below illustatres export inheritance where the base export provides metadata that is overriden by the inheritor
using an InheritedExport and ExportMetadata attributes. 

In this case OverridingExtension implements IExtension but then explicitly overrides the base and provides new metadata. 

The result is a single export rather than two exports.

*/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
 
 
namespace InheritedExportTest
{
    [TestClass]
    public class InheritedExportTests
    {
        [TestMethod]
        public void Overriding_extension_creates_a_single_export()
        {
            var catalog = new TypeCatalog(typeof (OverridingExtension));
            var container = new CompositionContainer(catalog);
            var extensions = container.GetExportedValues<IExtension>();
            Assert.AreEqual(1, extensions.Count()); //check that only one export exists
        }
    }
 
    [InheritedExport] //base export with no metadata
    public interface IExtension
    {
        
    }
 
    [ExportMetadata("Name", "OverridingExtension")] //metadata for the export
    [InheritedExport(typeof(IExtension))] //overrides the base export
    public class OverridingExtension : IExtension
    {
    }
}
by Glenn Block
  December 13, 2009 @ 11:03am

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate