static IdUtils() { #if !SILVERLIGHT && !MONOTOUCH && !XBOX var hasIdInterfaces = typeof(T).FindInterfaces( (t, critera) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IHasId <>), null); if (hasIdInterfaces.Length > 0) { CanGetId = HasId <T> .GetId; return; } #endif if (typeof(T).IsClass()) { if (typeof(T).GetPropertyInfo(IdUtils.IdField) != null && typeof(T).GetPropertyInfo(IdUtils.IdField).GetMethodInfo() != null) { CanGetId = HasPropertyId <T> .GetId; return; } foreach (var pi in typeof(T).GetPublicProperties() .Where(pi => pi.CustomAttributes() .Cast <Attribute>() .Any(attr => attr.GetType().Name == "PrimaryKeyAttribute"))) { CanGetId = StaticAccessors <T> .ValueUnTypedGetPropertyTypeFn(pi); return; } } CanGetId = x => x.GetHashCode(); }
static IdUtils() { #if !SL5 && !IOS && !XBOX #if NETSTANDARD1_3 var hasIdInterfaces = typeof(T).GetTypeInfo().ImplementedInterfaces.Where(t => t.GetTypeInfo().IsGenericType && t.GetTypeInfo().GetGenericTypeDefinition() == typeof(IHasId <>)).ToArray(); #else var hasIdInterfaces = typeof(T).FindInterfaces( (t, critera) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IHasId <>), null); #endif if (hasIdInterfaces.Length > 0) { CanGetId = HasId <T> .GetId; return; } #endif if (typeof(T).IsClass() || typeof(T).IsInterface()) { foreach (var pi in typeof(T).GetPublicProperties() .Where(pi => pi.AllAttributes <Attribute>() .Any(attr => attr.GetType().Name == "PrimaryKeyAttribute"))) { CanGetId = StaticAccessors <T> .ValueUnTypedGetPropertyTypeFn(pi); return; } var piId = typeof(T).GetIdProperty(); if (piId != null && piId.GetMethodInfo() != null) { CanGetId = HasPropertyId <T> .GetId; return; } } if (typeof(T) == typeof(object)) { CanGetId = x => { var piId = x.GetType().GetIdProperty(); if (piId != null && piId.GetMethodInfo() != null) { return(x.GetObjectId()); } return(x.GetHashCode()); }; return; } CanGetId = x => x.GetHashCode(); }
public void Can_get_field_accessor_from_sub_and_super_types() { var sub = new SubAccessor(); var subGet = StaticAccessors.GetValueGetter <SubAccessor>(typeof(SubAccessor).GetField("SubField")); var subSet = StaticAccessors.GetValueSetter <SubAccessor>(typeof(SubAccessor).GetField("SubField")); subSet(sub, "sub"); Assert.That(subGet(sub), Is.EqualTo("sub")); var sup = new AccessorBase(); var supGet = StaticAccessors.GetValueGetter <AccessorBase>(typeof(AccessorBase).GetField("BaseField")); var supSet = StaticAccessors.GetValueSetter <AccessorBase>(typeof(AccessorBase).GetField("BaseField")); supSet(sup, "base"); Assert.That(supGet(sup), Is.EqualTo("base")); supSet(sub, "base"); Assert.That(supGet(sub), Is.EqualTo("base")); }
static HasPropertyId() { var pi = typeof(TEntity).GetPropertyInfo(IdUtils.IdField); GetIdFn = StaticAccessors <TEntity> .ValueUnTypedGetPropertyTypeFn(pi); }
static HasPropertyId() { var pi = typeof(TEntity).GetIdProperty(); GetIdFn = StaticAccessors <TEntity> .ValueUnTypedGetPropertyTypeFn(pi); }