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

IViewMetadata for gettting TypeIdentity of Export

286 Views
Copy Code Show/Hide Line Numbers
namespace Nowcom.Quicksilver
{
    using System.ComponentModel.Composition.Primitives;
    using System.Linq;
    using System;
    using System.ComponentModel.Composition.ReflectionModel;
    using System.Collections.Generic;
 
    public class WrappedPartDefinition: ComposablePartDefinition
    {
        ComposablePartDefinition _PartDefinition;
        Dictionary<string, object> _Metadata;
 
        public WrappedPartDefinition(ComposablePartDefinition partDefintion, Type type)
        {
            _PartDefinition = partDefintion;
            _Metadata = new Dictionary<string, object>(partDefintion.Metadata);
            _Metadata["TypeIdentity"] = type;
        }
 
        public override IEnumerable<ExportDefinition> ExportDefinitions { get { return _PartDefinition.ExportDefinitions; } }
        public override IEnumerable<ImportDefinition> ImportDefinitions { get { return _PartDefinition.ImportDefinitions; } }
 
        public override IDictionary<string, object> Metadata { get { return _Metadata; } }
 
        public override ComposablePart CreatePart()
        {
            return _PartDefinition.CreatePart();
        }
    }
 
    public class MetadataCatalog : ComposablePartCatalog
    {
        ComposablePartCatalog _WrappedCatalog;
 
        public MetadataCatalog(ComposablePartCatalog wrappedCatalog)
        {
            if (wrappedCatalog == null)
            {
                throw new ArgumentNullException("wrappedCatalog");
            }
            _WrappedCatalog = wrappedCatalog;
        }
 
        public override IQueryable<ComposablePartDefinition> Parts
        {
            get
            {
                return _WrappedCatalog.Parts.Select(p => TransformPart(p));
            }
        }
 
        private ComposablePartDefinition TransformPart(ComposablePartDefinition definition)
        {
            Type type = ReflectionModelServices.GetPartType(definition).Value;
            var partDef = new WrappedPartDefinition(definition, type);
            
            return partDef;
        }
 
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _WrappedCatalog.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
 
namespace Nowcom.Quicksilver
{
    using System.ComponentModel.Composition;
    using System.Windows;
    using System;
    using System.ComponentModel.Composition.ReflectionModel;
    using System.ComponentModel.Composition.Primitives;
    using System.Linq;
    using System.Collections.Generic;
 
    public interface IViewMetadata
    {
        Type TypeIdentity { get; }
    }
 
    [MetadataAttribute]
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public sealed class ViewAttribute : ExportAttribute, IViewMetadata
    {
        public ViewAttribute()
            : base(typeof(UIElement))
        {
        }
 
        public Type TypeIdentity { get; set; }
    }
}
 
[Export]
public class ViewContainer 
{
  [ImportMany(typeof(UIElement), AllowRecomposition = true)]
  public List<ExportFactory<UIElement, IViewMetadata>> Views { get; set; }
}
 
by Robert Kozak
  April 08, 2010 @ 6:04pm
Tags:
Description:
Glenn, Here is the code for my MetadataCatalog, WrappedPartDefinition, IViewMetadata, ViewAttribute and ViewContainer.

Views are imported but TypeIdentity in the Metadata is null. My WrappedPartDefintion is adding the type I can see it in the debugger.

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