我想使用基于属性标记的映射机制,如:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class DomainSignatureAttribute : Attribute { }
public class SomeThing {
[DomainSignature]
public virtual string SomePropertyForMapping {get;set;}
[DomainSignature]
public virtual int OtherPropertyForMapping {get;set;}
public virtual string OtherPropertyWithoutMapping {get;}
}因此,在ClassMap子类中,我想要实现这样的方法,即映射所有属性,标记为DomainSignatureAttribute属性:
protected void MapPropertiesWithStandartType()
{
foreach (PropertyInfo property in typeof(T).GetProperties())
{
if (property != null)
{
object[] domainAttrs = property.GetCustomAttributes(typeof (DomainSignatureAttribute), true);
if (domainAttrs.Length > 0)
Map(x => property.GetValue(x, null));
}
}
}但是Linq有一个小问题。当FluentNHibernate构建映射(Cfg.BuildSessionFactory())时,它会中断
尝试在添加属性'GetValue‘时添加属性。
异常。当我取消时,我需要将Linq表达式:x => property.GetValue(x, null)重新构建成正确的形式。
请不要建议使用AutoMap特性,也不要建议使用手动映射。
发布于 2011-11-07 10:14:52
var domainproperties = typeof(T).GetProperties()
.Where(prop => prop.GetCustomAttributes(typeof (DomainSignatureAttribute), true).Length > 0);
foreach (var property in domainproperties)
{
Map(Reveal.Member<T>(property.Name));
}https://stackoverflow.com/questions/8017281
复制相似问题