Nhibernate mapped internal classes and InternalsVisibleTo

点点圈 提交于 2020-01-01 18:17:34

问题


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.Lot

StackTrace:

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 871 NHibernateUtilities.BaseUnitOfWork.getAllT in C:\Users\Isaac.G\Desktop\svn.bolinger.ca\Library Projects\NHibernateUtilities\NHibernateUtilities\BaseUnitOfWork.cs: line 115 TestProject1.UnitTest1.TestMethod1() in C:\Users\Isaac.G\Desktop\svn.bolinger.ca\Library Projects\BaseSystemCore\TestProject1\UnitTest1.cs: line 71

Has anyone ever got this to work before?

Thanks

Isaac


回答1:


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).




回答2:


For what its worth, this is what I've dropped into a T4. It automatically generates all the InternalsVisibleTo attributes for my entities. Hope it helps.

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\Domain\bin\Debug\Domain.dll" #>
<#@ import namespace="System.Linq" #>
<#@ output extension=".cs" #>
using System.Runtime.CompilerServices;

<#
 var publicType = typeof(Domain.Foo);
 var allTypes = publicType.Assembly.GetTypes();

 var entityType = allTypes.Single(t => t.FullName == "Domain.Entities.Entity"); // my entity supertype

 foreach(var type in allTypes.Where(t => !t.IsAbstract && entityType.IsAssignableFrom(t)))
{#>
[assembly: InternalsVisibleTo("<#= type.Name #>ProxyAssembly")]
<#}#>


来源:https://stackoverflow.com/questions/7151730/nhibernate-mapped-internal-classes-and-internalsvisibleto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!