diff --git a/src/Dapper.Contrib/SqlMapperExtensions.cs b/src/Dapper.Contrib/SqlMapperExtensions.cs index 9a30e805..5e4a52e7 100644 --- a/src/Dapper.Contrib/SqlMapperExtensions.cs +++ b/src/Dapper.Contrib/SqlMapperExtensions.cs @@ -127,7 +127,16 @@ private static List TypePropertiesCache(Type type) return pis.ToList(); } - var properties = type.GetProperties().Where(IsWriteable).ToArray(); + var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) + .Where(IsWriteable) + .Where(p => !IsPrivate(p)) + .ToArray(); + + if (properties.Length == 0) + { + throw new DataException($"Entity of type '{type.FullName}' has no accessible properties"); + } + TypeProperties[type.TypeHandle] = properties; return properties.ToList(); } @@ -141,6 +150,11 @@ private static bool IsWriteable(PropertyInfo pi) return writeAttribute.Write; } + private static bool IsPrivate(PropertyInfo pi) + { + return pi.GetGetMethod()?.IsPrivate ?? true; + } + private static PropertyInfo GetSingleKey(string method) { var type = typeof(T); @@ -156,7 +170,7 @@ private static PropertyInfo GetSingleKey(string method) } /// - /// Returns a single entity by a single id from table "Ts". + /// Returns a single entity by a single id from table "Ts". /// Id must be marked with [Key] attribute. /// Entities created from interfaces are tracked/intercepted for changes and used by the Update() extension /// for optimal performance.