我在我的应用程序中使用Unity 2.1,并在标题中收到一个解决方案的错误。我不知道在哪里看。
这个问题似乎与64位架构无关。非常感谢任何帮助!
//works: class = "ProductManager<Product>
Container.Resolve<IProductManager<Product>>()
//works: class = "OrderManager"
Container.Resolve<IOrderManager()
//works: class="OrderManager"
Container.Resolve<IOrderManager("OrderManager")
//DOESN'T WORK: EXCEPTION: BadImageFormatException
Container.Resolve<IOrderManager("OrderManager")
//works: class="GenericOrderManager<Order>" (obviously)
var manager = new GenericOrderManager<Order>();
Unity.config
<alias name="IProductManager" type="Assembly1.Namespace.IProductManager`1" />
<alias name="ProductManager" type="Assembly2.Namespace.ProductManager`1" />
<alias name="IOrderManager" type="Assembly1.Namespace.IOrderManager" />
<alias name="OrderManager"
type="Assembly1.Namespace.OrderManager" />
<alias name="OrderManager"
type="Assembly1.Namespace.OrderManager"
name="OrderManager" />
<alias name="GenericOrderManager"
type="Assembly2.Namespace.GenericOrderManager`1"
name="GenericOrderManager" />
ProductManager +界面
public interface IProductManager<TProduct> where TProduct : Product
{
}
public class ProductManager<TProduct> : IProductManager<TProduct> where TProduct : Product
{
}
OrderManager +界面
public interface IOrderManager
{
}
public class OrderManager : IOrderManager
{
}
public class OrderManager<TOrder> : OrderManager where TOrder : Order
{
}
使用StackTrace进行更新:
在 System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo 方法)在System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType)at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.GetBuildMethod() 在 Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context,NamedTypeBuildKey buildKey)at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext 上下文) Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext 上下文)在Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t,Object existing,String name,IEnumerable`1 resolverOverrides)
答案 0 :(得分:2)
您拥有的Unity
版本为x32
。您的项目程序集正在构建为x64
,但是将32位Unity程序集作为引用。不幸的是,编译会很好。不过,你会在运行时遇到令人讨厌的惊喜。底线:使用x86
的平台目标进行编译。
调用32位程序集的64位程序集是BadImageFormatException
的最常见原因之一。
MSDN说明:
DLL或可执行文件作为64位程序集加载,但它包含 32位功能或资源。例如,它依赖于COM互操作或 调用32位动态链接库中的方法。
要解决此异常,请设置项目的Platform目标属性 到x86(而不是x64或AnyCPU)并重新编译。
更多信息here。