Ejemplo n.º 1
0
        public PluginFamily(Type pluginType)
        {
            _pluginType = pluginType;

            resetDefault();
            _pluginType.GetTypeInfo().ForAttribute <StructureMapAttribute>(a => a.Alter(this));


            _instances.OnMissing = name => MissingInstance?.ToNamedClone(name);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// If the PluginType is an open generic type, this method will create a
        /// closed type copy of this PluginFamily
        /// </summary>
        /// <param name="templateTypes"></param>
        /// <returns></returns>
        public PluginFamily CreateTemplatedClone(Type[] templateTypes)
        {
            var templatedType   = _pluginType.MakeGenericType(templateTypes);
            var templatedFamily = new PluginFamily(templatedType);

            templatedFamily.copyLifecycle(this);

            _instances.GetAll().Select(x => {
                var clone = x.CloseType(templateTypes);
                if (clone == null)
                {
                    return(null);
                }

                clone.Name = x.Name;
                return(clone);
            }).Where(x => x != null).Each(templatedFamily.AddInstance);

            if (GetDefaultInstance() != null)
            {
                var defaultKey = GetDefaultInstance().Name;
                var @default   = templatedFamily.Instances.FirstOrDefault(x => x.Name == defaultKey);
                if (@default != null)
                {
                    templatedFamily.SetDefault(@default);
                }
            }

            if (MissingInstance != null)
            {
                templatedFamily.MissingInstance = MissingInstance.CloseType(templateTypes);
            }

            //Are there instances that close the templatedtype straight away?
            _instances.GetAll()
            .Where(x => x.ReturnedType.CanBeCastTo(templatedType))
            .Each(templatedFamily.AddInstance);

            return(templatedFamily);
        }