编程语言
首页 > 编程语言> > c# – 如何使用代码“约定”映射忽略属性映射

c# – 如何使用代码“约定”映射忽略属性映射

作者:互联网

有没有办法避免使用NHibernate 3.2使用代码约定映射来映射属性?默认情况下,映射所有属性.

解决方法:

据我所知,有两种选择:

1)扩展ConventionModelMapper和SimpleModelInspector以扩展IsPersistentProperty以满足您的需要.

2)使用IsPersistentProperty如下:

...
mapper.IsPersistentProperty((memberInfo, declared) => IsPersistentProperty(mapper.ModelInspector, memberInfo, declared, "YourPropertyName"));
...


public static bool IsPersistentProperty(IModelInspector modelInspector, MemberInfo member, bool declared, string propertyName)
{
    return (declared ||(member is PropertyInfo) && !IsReadOnlyProperty(member)) && !member.Name.Equals(propertyName);
}

private static bool IsReadOnlyProperty(MemberInfo subject)
{
    const BindingFlags defaultBinding = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;

    var property = subject as PropertyInfo;
    if (property == null)
    {
        return false;
    }
    if (CanReadCantWriteInsideType(property) || CanReadCantWriteInBaseType(property))
    {
        return !PropertyToField.DefaultStrategies.Values.Any(s => subject.DeclaringType.GetField(s.GetFieldName(property.Name), defaultBinding) != null) || IsAutoproperty(property);
    }
    return false;
}

private static bool IsAutoproperty(PropertyInfo property)
{
    return property.ReflectedType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
                                                                             | BindingFlags.DeclaredOnly).Any(pi => pi.Name == string.Concat("<", property.Name, ">k__BackingField"));
}

private static bool CanReadCantWriteInsideType(PropertyInfo property)
{
    return !property.CanWrite && property.CanRead && property.DeclaringType == property.ReflectedType;
}

private static bool CanReadCantWriteInBaseType(PropertyInfo property)
{
    if (property.DeclaringType == property.ReflectedType)
    {
        return false;
    }
    var rfprop = property.DeclaringType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
                                                                             | BindingFlags.DeclaredOnly).SingleOrDefault(pi => pi.Name == property.Name);
    return rfprop != null && !rfprop.CanWrite && rfprop.CanRead;
}

标签:c,nhibernate,nhibernate-mapping
来源: https://codeday.me/bug/20190621/1252762.html