Inheritance 重复的继承属性 [InheritedExport] [登录数据] 公共接口ILogon { bool身份验证(字符串用户名、字符串密码); } 公共接口元数据 { 字符串名 { 得到; } 字符串描述 { 得到; } } [元数据属性] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface,AllowMultiple=false,Inherited=true)] 公共类LoginMetadata属性:ExportAttribute,ILoginMetadata { 公共LoginMetadataAttribute() :base(typeof(ILogon)) { } #区域元数据成员 公共字符串名 { 得到; 设置 } 公共字符串描述 { 得到; 设置 } #端区 } 公共类登录 { [进口数量(类型(ILogon))] 公共列表插件 { 得到; 设置 } } 公共静态类登录 { 私有静态LogonPlugins\u Plugins=null; 私有静态聚合Catalog_Catalog=null; 私有静态合成容器_Container=null; 公共静态登录插件 { 得到 { if(_Plugins==null) { _插件=新登录插件(); _Catalog=newaggregateCatalog(); _Catalog.Catalogs.Add(新的AssemblyCatalog(Assembly.getExecutionGassembly()); _容器=新的合成容器(_目录); _Container.ComposeParts(_插件); } 返回(_插件); } } }

Inheritance 重复的继承属性 [InheritedExport] [登录数据] 公共接口ILogon { bool身份验证(字符串用户名、字符串密码); } 公共接口元数据 { 字符串名 { 得到; } 字符串描述 { 得到; } } [元数据属性] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface,AllowMultiple=false,Inherited=true)] 公共类LoginMetadata属性:ExportAttribute,ILoginMetadata { 公共LoginMetadataAttribute() :base(typeof(ILogon)) { } #区域元数据成员 公共字符串名 { 得到; 设置 } 公共字符串描述 { 得到; 设置 } #端区 } 公共类登录 { [进口数量(类型(ILogon))] 公共列表插件 { 得到; 设置 } } 公共静态类登录 { 私有静态LogonPlugins\u Plugins=null; 私有静态聚合Catalog_Catalog=null; 私有静态合成容器_Container=null; 公共静态登录插件 { 得到 { if(_Plugins==null) { _插件=新登录插件(); _Catalog=newaggregateCatalog(); _Catalog.Catalogs.Add(新的AssemblyCatalog(Assembly.getExecutionGassembly()); _容器=新的合成容器(_目录); _Container.ComposeParts(_插件); } 返回(_插件); } } },inheritance,interface,attributes,mef,Inheritance,Interface,Attributes,Mef,这段代码意味着使用惰性模型加载ILogon类型的扩展对象。 由于懒惰的方式,对象必须继承ILogon,并且必须还声明了LoginMetadata属性on。 我真的不想强制LoginMetadata,所以我声明它是可继承的,并将其添加到接口中,不带任何参数。 我的问题是,当我在plugin类上声明属性时,最终列表将包含两个相同插件类型的条目。一个用于接口上声明并继承的空属性;一个用于插件类上声明的属性 有办法克服吗 谢谢 Peter其中一个导出来自应用于ILogin接口的InheritedExpo

这段代码意味着使用惰性模型加载ILogon类型的扩展对象。 由于懒惰的方式,对象必须继承ILogon,并且必须还声明了LoginMetadata属性on。 我真的不想强制LoginMetadata,所以我声明它是可继承的,并将其添加到接口中,不带任何参数。 我的问题是,当我在plugin类上声明属性时,最终列表将包含两个相同插件类型的条目。一个用于接口上声明并继承的空属性;一个用于插件类上声明的属性

有办法克服吗

谢谢
Peter

其中一个导出来自应用于ILogin接口的InheritedExport,第二个导出来自LoginMetadataAttribute属性,该属性也是一个ExportAttribute。您需要从LoginMetadataAttribute中删除InheritedExport或ExportAttribute基类型

通常,如果您使用的是元数据,那么它应该是特定于给定导出的,因此我希望您应该从接口中删除InheritedExport,并希望导出ILogin的人应用LoginMetadata属性并提供必要的元数据。如果这是您选择的路线,我建议将其重命名为LoginExportAttribute

[InheritedExport]
[LoginMetadata]
public interface ILogon
{
    bool Authenticate ( string UserName, string Password );
}

public interface ILoginMetadata
{
    string Name
    {
        get;
    }

    string Description
    {
        get;
    }
}

[MetadataAttribute]
[AttributeUsage( AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = false, Inherited = true )]
public class LoginMetadataAttribute : ExportAttribute, ILoginMetadata
{
    public LoginMetadataAttribute ( )
        : base( typeof( ILogon ) )
    {
    }

    #region ILoginMetadata Members

    public string Name
    {
        get;
        set;
    }

    public string Description
    {
        get;
        set;
    }

    #endregion
}

public class LogonPlugins
{
    [ImportMany( typeof( ILogon ) )]
    public List<Lazy<ILogon, ILoginMetadata>> Plugins
    {
        get;
        set;
    }
}

public static class Logon
{
    private static LogonPlugins _Plugins = null;
    private static AggregateCatalog _Catalog = null;
    private static CompositionContainer _Container = null;

    public static LogonPlugins Plugins
    {
        get
        {
            if ( _Plugins == null )
            {
                _Plugins = new LogonPlugins( );

                _Catalog = new AggregateCatalog( );
                _Catalog.Catalogs.Add( new AssemblyCatalog( Assembly.GetExecutingAssembly( ) ) );

                _Container = new CompositionContainer( _Catalog );
                _Container.ComposeParts( _Plugins );
            }

            return ( _Plugins );
        }
    }
}