我遇到了GetExports<T,TMetadata>()
的非常奇怪的行为。
我有以下代码:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true), MetadataAttribute]
public class ControllerExportAttribute : ExportAttribute, IControllerMetadata
{
public ControllerExportAttribute(Type controllerType, SessionStateBehavior controllerSessionStateBehavior)
: base(typeof(IController))
{
ControllerSessionStateBehavior = controllerSessionStateBehavior;
ControllerName = controllerType.Name;
}
public ControllerExportAttribute(Type controllerType)
: this(controllerType, SessionStateBehavior.Default)
{
}
public string ControllerName { get; private set; }
public SessionStateBehavior ControllerSessionStateBehavior { get; private set; }
}
public interface IControllerMetadata
{
//string ControllerName { get; }
//SessionStateBehavior ControllerSessionStateBehavior { get; }
}
我有以下行使用属性导入控制器:
var exportedControllers = ServiceLocator.Container.GetExports<IController, IControllerMetadata>();
现在到了奇怪的事情。如果我从界面中的字段中删除注释,则导入不起作用。
也许我错过了什么......