private AdoProviderMetadata(Type connectionType) { this.connectionType = connectionType; var createCommandMethod = GetPublicInstanceDeclaredOnlyMethod(connectionType, nameof(DbConnection.CreateCommand)); if (createCommandMethod == null) { throw new NotSupportedException(); } var commandType = createCommandMethod.ReturnType; var parametersProperty = commandType.GetProperty(nameof(DbCommand.Parameters), PublicInstanceDeclaredOnly); if (parametersProperty == null) { throw new NotSupportedException(); } var parameterCollectionType = parametersProperty.PropertyType; var executeReaderMethod = GetPublicInstanceDeclaredOnlyMethod(commandType, nameof(DbCommand.ExecuteReader)); if (executeReaderMethod == null) { throw new NotSupportedException(); } var dataReaderType = executeReaderMethod.ReturnType; var createParameterMethod = GetPublicInstanceDeclaredOnlyMethod(commandType, nameof(DbCommand.CreateParameter)); if (createParameterMethod == null) { throw new NotSupportedException(); } var parameterType = createParameterMethod.ReturnType; ParameterConstructor = parameterType.GetConstructor(ParameterConstructorParameters); if (ParameterConstructor == null) { throw new NotSupportedException(); } ParameterCollectionAddMethod = parameterCollectionType.GetMethod(nameof(DbParameterCollection.Add), new[] { parameterType }); if (ParameterCollectionAddMethod == null) { throw new NotSupportedException(); } DataReaderMetadata = new DataReaderMetadata(dataReaderType); if (DataReaderMetadata == null) { throw new NotSupportedException(); } }
public DataReaderValueDeserializer(DataReaderMetadata dataReaderMetadata, MethodInfo getValueMethod, Type type, int ordinal) { this.dataReaderMetadata = dataReaderMetadata; this.getValueMethod = getValueMethod; this.type = type; this.ordinal = ordinal; nullableInfo = NullableInfo.GetNullable(type); }
private static void EmitDeserializeUserType(DataReaderMetadata dataReaderMetadata, FieldMap fieldMap, ILGenerator il, Type type, string[] columnNames, Type[] columnTypes) { var constructor = type.GetConstructor(Type.EmptyTypes); if (constructor == null) { ThrowException.NoDefaultConstructor(); } var sample = Activator.CreateInstance(type); il.Emit(OpCodes.Newobj, constructor); var uniqueNames = new HashSet <string>(); for (var ordinal = 0; ordinal < columnNames.Length; ordinal++) { var propertyInfo = type.GetProperty(columnNames[ordinal], BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); if (propertyInfo == null) { continue; } var getValueMethod = dataReaderMetadata.GetGetValueMethodFromType(columnTypes[ordinal]); if (propertyInfo.CanWrite && propertyInfo.GetSetMethod() != null) { var propertyIsNullInitialized = propertyInfo.CanRead && propertyInfo.GetValue(sample) == null; var hasRequiredAttribute = PropertyHasRequiredAttribute(propertyInfo); var dataReaderPropertyValueDeserializer = new DataReaderValueDeserializer(dataReaderMetadata, getValueMethod, propertyInfo, ordinal); dataReaderPropertyValueDeserializer.EmitDeserializeProperty(il, fieldMap, propertyIsNullInitialized, hasRequiredAttribute); if (uniqueNames.Add(propertyInfo.Name) == false) { ThrowException.DuplicateFieldNames(propertyInfo.Name); } continue; } var fieldInfo = type.GetField("<" + propertyInfo.Name + ">k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic); if (fieldInfo != null) { var fieldIsNullInitialized = fieldInfo.GetValue(sample) == null; var hasRequiredAttribute = PropertyHasRequiredAttribute(propertyInfo); var dataReaderFieldValueDeserializer = new DataReaderValueDeserializer(dataReaderMetadata, getValueMethod, fieldInfo, ordinal); dataReaderFieldValueDeserializer.EmitDeserializeProperty(il, fieldMap, fieldIsNullInitialized, hasRequiredAttribute); if (uniqueNames.Add(propertyInfo.Name) == false) { ThrowException.DuplicateFieldNames(propertyInfo.Name); } } } }
private static void EmitDeserializeType(DataReaderMetadata dataReaderMetadata, FieldMap fieldMap, ILGenerator il, Type type, string[] columnNames, Type[] columnTypes) { if (columnNames == DbQuery.NoColumnNames) { EmitDeserializeClrType(dataReaderMetadata, fieldMap, il, columnTypes[0], type); } else { EmitDeserializeUserType(dataReaderMetadata, fieldMap, il, type, columnNames, columnTypes); } }
public DataReaderValueDeserializer(DataReaderMetadata dataReaderMetadata, MethodInfo getValueMethod, PropertyInfo propertyInfo, int ordinal) : this(dataReaderMetadata, getValueMethod, propertyInfo.PropertyType, ordinal) { this.propertyInfo = propertyInfo; }
public DataReaderValueDeserializer(DataReaderMetadata dataReaderMetadata, MethodInfo getValueMethod, FieldInfo fieldInfo, int ordinal) : this(dataReaderMetadata, getValueMethod, fieldInfo.FieldType, ordinal) { this.fieldInfo = fieldInfo; }
private static void EmitDeserializeClrType(DataReaderMetadata dataReaderMetadata, FieldMap fieldMap, ILGenerator il, Type sourceType, Type type) { var getValueMethod = dataReaderMetadata.GetGetValueMethodFromType(sourceType); new DataReaderValueDeserializer(dataReaderMetadata, getValueMethod, type, 0).EmitDeserializeClrType(il, fieldMap, false); }