We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Like Dapper
/// <summary> /// Finds best constructor /// </summary> /// <param name="names">DataReader column names</param> /// <param name="types">DataReader column types</param> /// <returns>Matching constructor or default one</returns> public ConstructorInfo FindConstructor(string[] names, Type[] types) { var constructors = _type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (ConstructorInfo ctor in constructors.OrderBy(c => c.IsPublic ? 0 : (c.IsPrivate ? 2 : 1)).ThenBy(c => c.GetParameters().Length)) { ParameterInfo[] ctorParameters = ctor.GetParameters(); if (ctorParameters.Length == 0) return ctor; if (ctorParameters.Length != types.Length) continue; int i = 0; for (; i < ctorParameters.Length; i++) { if (!string.Equals(ctorParameters[i].Name, names[i], StringComparison.OrdinalIgnoreCase)) break; if (types[i] == typeof(byte[]) && ctorParameters[i].ParameterType.FullName == SqlMapper.LinqBinary) continue; var unboxedType = Nullable.GetUnderlyingType(ctorParameters[i].ParameterType) ?? ctorParameters[i].ParameterType; if ((unboxedType != types[i] && !SqlMapper.HasTypeHandler(unboxedType)) && !(unboxedType.IsEnum && Enum.GetUnderlyingType(unboxedType) == types[i]) && !(unboxedType == typeof(char) && types[i] == typeof(string)) && !(unboxedType.IsEnum && types[i] == typeof(string))) { break; } } if (i == ctorParameters.Length) return ctor; } return null; } /// <summary> /// Returns the constructor, if any, that has the ExplicitConstructorAttribute on it. /// </summary> public ConstructorInfo FindExplicitConstructor() { var constructors = _type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var withAttr = constructors.Where(c => c.GetCustomAttributes(typeof(ExplicitConstructorAttribute), true).Length > 0).ToList(); if (withAttr.Count == 1) { return withAttr[0]; } return null; } /// <summary> /// Gets mapping for constructor parameter /// </summary> /// <param name="constructor">Constructor to resolve</param> /// <param name="columnName">DataReader column name</param> /// <returns>Mapping implementation</returns> public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName) { var parameters = constructor.GetParameters(); return new SimpleMemberMap(columnName, parameters.FirstOrDefault(p => string.Equals(p.Name, columnName, StringComparison.OrdinalIgnoreCase))); }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Like Dapper
The text was updated successfully, but these errors were encountered: