[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 );
        }
    }
}
此代码旨在使用 Lazy 模型加载 ILogon 类型的扩展对象。因为 Lazy 的方式,对象必须继承 ILogon 并且必须还声明了 LoginMetadata 属性。我真的不想强制使用 LoginMetadata,所以我声明它是可继承的,并将其添加到没有参数的接口中。我的问题是,当我在插件类上声明属性时,最终列表将包含两个相同插件类型的条目。一个用于在接口上声明的空属性 - 并继承;一个用于插件类上声明的属性。
有办法克服吗?
谢谢彼得