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 using a custom metadata attribute

801 Views
Copy Code Show/Hide Line Numbers
/*

Sample below illustates export inheritance where the base export provides metadata that is overriden by the inheritor
using a custom metadata attribute. 

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.

Notice the inheritor is using an InheritedExport attribute + a custom metadata attribute to override rather than using a custom InheritedExport. 
This is due to a bug in how overriding works with InheritedExport.

Additionally notice we are using a metadata view (IExtensionMetadata) to access the metadata in a strongly typed fashion

*/
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 CustomInheritedExportTests
{
    [TestClass]
    public class CustomInheritedExportTests
    {
        private CompositionContainer _container;
 
        [TestInitialize]
        public void Setup()
        {
            var catalog = new TypeCatalog(typeof(OverridingExtension));
            _container = new CompositionContainer(catalog);
        }
 
        [TestMethod]
        public void Overriding_extension_creates_a_single_export()
        {
            var extensions = _container.GetExports<IExtension>();
            Assert.AreEqual(1, extensions.Count()); //check that there is only one
        }
 
        [TestMethod]
        public void Overriding_metadata_replaces_inherited_metadata()
        {
            var extensions = _container.GetExports<IExtension, IExtensionMetadata>(); //use a strongly typed metadata view
            var extension = extensions.First();
            Assert.AreEqual(null, extension.Metadata.Category);
            Assert.AreEqual("OverridingExtension", extension.Metadata.Name);
        }
 
    }
 
    [Extension(Name="None", Category="None")] //base provides dummy metadata
    [InheritedExport]
    public interface IExtension
    {
 
    }
 
    public interface IExtensionMetadata //strongly typed metadata view
    {
        string Name { get; }
        string Category { get; }
    }
 
    [Extension(Name="OverridingExtension")] //override the name, category will be null.
    [InheritedExport(typeof(IExtension))]
    public class OverridingExtension : IExtension
    {
    }
 
    [MetadataAttribute]
    [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
    public class ExtensionAttribute : Attribute //not a custom export but a custom metadata attribute
    {
        public string Name { get; set; }
        public string Category { get; set; }
    }
 
}
 
by Glenn Block
  December 13, 2009 @ 1:34pm
Tags:

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