错误:测试方法TestProject1.UnitTest1.TestMethod1引发异常:NHibernate.HibernateException:创建代理实例失败---> Castle.DynamicProxy.Generators.GeneratorException:类型不是公共的,因此无法生成代理.类型:BaseSystemCore.Domain.Lot StackTrace: Castle.DynamicProxy.DefaultProxyBuilder.AssertValidType(类型目标)Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxy(类型classToProxy,类型[] AdditionalInterfacesToProxy,ProxyGenerationOptions选项)Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy,Type [] AdditionalInterfacesToProxy,ProxyGenerationOptions选项,Object []构造函数参数,IInterceptor []拦截器)NHibernate.ByteCode.Castle.ProxyFactory.GetProxy(对象ID,ISessionImplementor会话)NHibernateUtilities.BaseUnitOfWork.handleException(Exception e)in C:\ Users \ Isaac.G \ Desktop \ svn.bolinger.ca \ Library Projects \ NHibernateUtilities \ NHibernateUtilities \ BaseUnitOfWork.cs:871行C:\ Users \ Isaac.G \ Desktop \ svn.bolinger.ca \ Library Projects \ NHibernateUtilities \ NHibernateUtilities \ BaseUnitOfWork.cs中的NHibernateUtilities.BaseUnitOfWork.getAllT:第115行C:\ Users \ Isaac.G \ Desktop \ svn.bolinger.ca \ Library Projects \ BaseSystemCore \ TestProject1 \ UnitTest1.cs中的TestProject1.UnitTest1.TestMethod1():第71行有人有没有以前工作过?谢谢以撒解决方案 NHibernate 3.2内置了代理提供程序.我简要地查看了源代码,似乎它们使用了以下格式:{0} ProxyAssembly.其中{0}是惰性映射类的类型名称.我还没有测试过.尝试在您的AssemblyInfo.cs中添加以下内容,并将{0}替换为您的惰性类名称:[assembly: InternalsVisibleTo("{0}ProxyAssembly")]如果您仍然想使用旧版本的NHibernate,则可以尝试使用此版本(适用于城堡字节码提供程序):[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] DynamicProxyGenAssembly2是由Castle快速生成的临时程序集.它包含从映射的类(代理)派生的类.My proxy generator is having trouble generating proxies for internal mapped Nhibernate classes. I have tried adding them as visible using InternalsVisibleTo in assemblyinfo.cs but it doesn't seem to work. Worse, I don't know how to tell if I've even successfully managed to friend the proxy assemblies I want to because if I change a few numbers in the proxy assemblies public key in assemblyinfo.cs there is no error thrown.Error:Test method TestProject1.UnitTest1.TestMethod1 threw exception:NHibernate.HibernateException: Creating a proxy instance failed ---> Castle.DynamicProxy.Generators.GeneratorException: Type is not public, so a proxy cannot be generated. Type: BaseSystemCore.Domain.LotStackTrace:Castle.DynamicProxy.DefaultProxyBuilder.AssertValidType(Type target)Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)NHibernate.ByteCode.Castle.ProxyFactory.GetProxy(Object id, ISessionImplementor session)NHibernateUtilities.BaseUnitOfWork.handleException(Exception e) in C:\Users\Isaac.G\Desktop\svn.bolinger.ca\Library Projects\NHibernateUtilities\NHibernateUtilities\BaseUnitOfWork.cs: line 871NHibernateUtilities.BaseUnitOfWork.getAllT in C:\Users\Isaac.G\Desktop\svn.bolinger.ca\Library Projects\NHibernateUtilities\NHibernateUtilities\BaseUnitOfWork.cs: line 115TestProject1.UnitTest1.TestMethod1() in C:\Users\Isaac.G\Desktop\svn.bolinger.ca\Library Projects\BaseSystemCore\TestProject1\UnitTest1.cs: line 71Has anyone ever got this to work before?ThanksIsaac 解决方案 NHibernate 3.2 has built in proxy provider. I briefly look at the sources at it seems like they use this format: {0}ProxyAssembly. Where {0} is a type name for lazy mapped class. I have not tested it though. Try adding following to your AssemblyInfo.cs and replace {0} with your lazy class name:[assembly: InternalsVisibleTo("{0}ProxyAssembly")]If you still want to use older version of NHibernate you can try using this (for castle byte code provider):[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]DynamicProxyGenAssembly2 is a temporary assembly that gets generated on the fly by Castle. It contains classes derived from your mapped classes (proxies). 这篇关于Nhibernate映射内部类和InternalsVisibleTo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-12 05:33