gpt4 book ai didi

c# - Unity v3 - RegistrationByConvention - 无法加载类型异常

转载 作者:行者123 更新时间:2023-12-05 07:55:20 32 4
gpt4 key购买 nike

我正在使用 Enterprise Library 6 和 Unity v.3.5.0.0

出现以下错误:

Could not load type 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationSectionManageabilityProviderAttribute' from assembly 'Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

当使用此代码通过使用一组规则和约定(Registration by Convention)自动向容器注册多个类型时

container.RegisterTypes(
AllClasses.FromLoadedAssemblies()
.Where(x => (x.IsPublic == true) &&
(x.GetInterfaces().Any() == true) &&
(x.IsAbstract == false) &&
(x.IsClass == true) &&
x.Namespace == "Company.Project.Data.DA.NW" ),
WithMappings.FromAllInterfaces, type => (container.Registrations.Select(x => x.RegisteredType)
.Any(r => type.GetInterfaces().Contains(r) == true) == true) ? WithName.TypeName(type) : WithName.Default(type),
WithLifetime.Transient);

更新:

类型:

Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationSectionManageabilityProviderAttribute

不再是 EntLib 6 的一部分,而是在 Entlib 5 中。

堆栈跟踪:

[TypeLoadException: Could not load type 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationSectionManageabilityProviderAttribute' from assembly 'Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.]
System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) +0
System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) +145
System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) +162
System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) +87
System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) +438 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType) +103
System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit) +64
System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit) +87
System.Attribute.GetCustomAttribute(Assembly element, Type attributeType, Boolean inherit) +13
System.Reflection.CustomAttributeExtensions.GetCustomAttribute(Assembly element) +57
Microsoft.Practices.Unity.AllClasses.IsSystemAssembly(Assembly a) +78 Microsoft.Practices.Unity.<>c__DisplayClass10.b__f(Assembly a) +72 System.Linq.WhereArrayIterator1.MoveNext() +48<br/>
System.Linq.<SelectManyIterator>d__14
2.MoveNext() +168
System.Linq.WhereEnumerableIterator1.MoveNext() +152<br/>
Microsoft.Practices.Unity.UnityContainerRegistrationByConventionExtensions.RegisterTypes(IUnityContainer
container, IEnumerable
1 types, Func2 getFromTypes, Func2 getName, Func2 getLifetimeManager, Func2 getInjectionMembers, Boolean overwriteExistingMappings) +1323

最佳答案

这不是真正的解决方案,而是一种解决方法:

我简单地替换了第一个参数:

AllClasses.FromLoadedAssemblies()
.Where(x => (x.IsPublic == true) &&
(x.GetInterfaces().Any() == true) &&
(x.IsAbstract == false) &&
(x.IsClass == true) &&
x.Namespace == "Company.Project.Data.DA.NW" )

并用自定义函数替换它(assFilter 是一个字符串列表,带有简单的程序集名称,classFilter 是一个 namespace 列表)

var filteredAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => assFilter.Contains(a.FullName.Split(',')[0]));

List<Type> allClasses = new List<Type>();
foreach (var assembly in filteredAssemblies)
{
var classArray = assembly.GetTypes().Where(t => t.IsPublic &&
!t.IsAbstract &&
t.IsClass == true &&
classFilter.Contains(t.Namespace));
if (classArray != null && classArray.Count() > 0)
allClasses.AddRange(classArray);
}

关于c# - Unity v3 - RegistrationByConvention - 无法加载类型异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29917301/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com