Abstract properties in base classes getting added via AutoMap... code fix
Hi-
I had an issue where an abstract property from a base class kept showing up in my automapping, even though I was telling mapping to ignore it. Here's some pseudo-code:
abstract class a
{
public abstract int country { get; set; }
public int planet { get; set; }
}
class b : a
{
[AutoMapIgnore]
public override int country { get; set; }
}
// relevant part of the config showing how I'm using IgnoreProperty:
config = config.Mappings(x =>
{
x.AutoMappings.Add(
AutoMap.Assembly(MappedAssembly)
.OverrideAll(m => m.IgnoreProperties(p => p.MemberInfo.GetCustomAttributes(typeof(AutoMapIgnore), false).Count() > 0))
The country property from class b would get ignored correctly, but when Fluent reflected through the base class in MemberExtensions.GetInstanceMembers(), it would add a second instance of country to the list. OverrideAll and IgnoreProperty("country") also didn't work.
I traced this down, and here's what fixes the problem. In member.cs, MemberExtensions class, you don't want to add abstract properties to the member list:
public static IEnumerable<Member> GetInstanceProperties(this Type type)
{
foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
MethodInfo getter = property.GetGetMethod();
MethodInfo setter = property.GetSetMethod();
if ( ((getter == null) || (getter.IsAbstract == false))
&& ((setter == null) || (setter.IsAbstract == false)) )
{
yield return property.ToMember();
}
}
}
I was working with build 1.1.0.686.
Thanks.
--s