简单的注入器Webforms,属性注入masterpages

时间:2016-01-26 23:59:08

标签: c# asp.net webforms simple-injector

我已经遵循了webforms集成的设置准则,我正在注入Pages,没有任何问题。但我不能让属性注入在母版页上工作。我已略微修改了注册以考虑母版页,但导入的属性始终为null。以下是我所做的更改。

private static void RegisterWebPages()
{
    var pageTypes =
        from assembly in BuildManager.GetReferencedAssemblies().Cast<Assembly>()
        where !assembly.IsDynamic
        where !assembly.GlobalAssemblyCache
        from type in assembly.GetExportedTypes()
        where type.IsSubclassOf(typeof(Page)) || type.IsSubclassOf(typeof(MasterPage))
        where !type.IsAbstract && !type.IsGenericType
        select type;

    foreach (Type type in pageTypes)
    {
        var registration = Lifestyle.Transient.CreateRegistration(type, SimpleContainer);
        registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent, 
            "ASP.NET creates and disposes page classes for us.");
        SimpleContainer.AddRegistration(type, registration);
    }
}

class ImportAttributePropertySelectionBehavior : IPropertySelectionBehavior
{
    public bool SelectProperty(Type serviceType, PropertyInfo propertyInfo)
    {
        var propertyName = propertyInfo.Name;
        var isAssignable = (typeof(Page).IsAssignableFrom(serviceType) 
            || typeof(MasterPage).IsAssignableFrom(serviceType));
        var hasImportAttribute = propertyInfo.GetCustomAttributes<ImportAttribute>().Any();

        // Makes use of the System.ComponentModel.Composition assembly
        return isAssignable && hasImportAttribute;
    }
}

有什么想法吗?

0 个答案:

没有答案