public object GetParameterValue(int parameterIndex) { if (this.parameters == null || parameterIndex < 0 || parameterIndex > this.parameters.Count) { throw Error.ArgumentOutOfRange("parameterIndex"); } // ADO.NET providers require all results to be read before output parameters are visible if (this.session != null && !this.session.IsBuffered) { this.session.Buffer(); } SqlParameterInfo pi = this.parameters[parameterIndex]; object parameterValue = this.command.Parameters[parameterIndex].Value; if (parameterValue == DBNull.Value) { parameterValue = null; } if (parameterValue != null && parameterValue.GetType() != pi.Parameter.ClrType) { return(DBConvert.ChangeType(parameterValue, pi.Parameter.ClrType)); } return(parameterValue); }
public static IEnumerable <TOutput> Convert <TOutput>(IEnumerable source) { foreach (object value in source) { yield return(DBConvert.ChangeType <TOutput>(value)); } }
protected override object GetParameterValue(IProviderType type, object value) { if (value == null) { return(DBNull.Value); } var type2 = value.GetType(); //if (type2 == typeof(Guid)) if (type == GuidType) { return(DBConvert.ChangeType(value, typeof(byte[]))); } var closestRuntimeType = type.GetClosestRuntimeType(); if (closestRuntimeType == typeof(OracleDecimal)) { return(new OracleDecimal(Convert.ToDecimal(value))); } if (type2 == typeof(char) && closestRuntimeType == typeof(decimal)) { value = DBConvert.ChangeType(value, typeof(int)); return(DBConvert.ChangeType(value, closestRuntimeType)); } return(closestRuntimeType == type2 ? value : DBConvert.ChangeType(value, closestRuntimeType)); }
/// <summary> /// Changes the type of each element in a specified sequence. /// </summary> /// <typeparam name="TOutput">The type to convert the elements to.</typeparam> /// <param name="source">A sequence that contains elements to convert.</param> /// <returns>A sequence that contains the type-converted elements.</returns> public static IEnumerable <TOutput> Convert <TOutput>(IEnumerable source) { //<Convert>d__0<TDataReader, TOutput> d__ = new <Convert>d__0<TDataReader, TOutput>(-2); //d__.<>3__source = source; //return d__; foreach (var item in source) { yield return(DBConvert.ChangeType <TOutput>(item)); } }
protected virtual object GetParameterValue(IProviderType type, object value) { if (value == null) { return(DBNull.Value); } var type2 = value.GetType(); var closestRuntimeType = type.GetClosestRuntimeType(); return(closestRuntimeType == type2 ? value : DBConvert.ChangeType(value, closestRuntimeType)); }
private SqlExpression CoerceValueForExpression(SqlValue value, SqlExpression expression) { object clrValue = value.Value; if (!value.ClrType.IsAssignableFrom(expression.ClrType)) { clrValue = DBConvert.ChangeType(clrValue, expression.ClrType); } ProviderType newSqlType = typeProvider.ChangeTypeFamilyTo(value.SqlType, expression.SqlType); return(sql.Value(expression.ClrType, newSqlType, clrValue, value.IsClientSpecified, value.SourceExpression)); }
[ResourceConsumption(ResourceScope.Assembly | ResourceScope.Machine)] // InitDerivedTypes method call. private MetaType InitInheritedType(TypeMapping typeMap, MappedType type) { this.derivedTypes.Add(type.Type, type); if (typeMap.InheritanceCode != null) { // Mapping with no inheritance code: For example, an unmapped intermediate class in a hierarchy. if (this.Discriminator == null) { throw Error.NoDiscriminatorFound(type.Name); } if (type.Type.IsAbstract) { throw Error.AbstractClassAssignInheritanceDiscriminator(type.Type); } object keyValue = DBConvert.ChangeType(typeMap.InheritanceCode, this.Discriminator.Type); foreach (object d in inheritanceCodes.Keys) { // if the keys are equal, or if they are both strings containing only spaces // they are considered equal if ((keyValue.GetType() == typeof(string) && ((string)keyValue).Trim().Length == 0 && d.GetType() == typeof(string) && ((string)d).Trim().Length == 0) || object.Equals(d, keyValue)) { throw Error.InheritanceCodeUsedForMultipleTypes(keyValue); } } if (type.InheritanceCode != null) { throw Error.InheritanceTypeHasMultipleDiscriminators(type); } type.SetInheritanceCode(keyValue); this.inheritanceCodes.Add(keyValue, type); if (typeMap.IsInheritanceDefault) { if (this.inheritanceDefault != null) { throw Error.InheritanceTypeHasMultipleDefaults(type); } this.inheritanceDefault = type; } } // init sub-inherited types foreach (TypeMapping tm in typeMap.DerivedTypes) { this.InitDerivedTypes(tm); } return(type); }
public override V GetValue(T instance) { if (isItem) { var value = this.dItemGet(instance, key); if (value == null) { return(default(V)); } return((V)DBConvert.ChangeType(value, typeof(V))); } return(this.dget(instance)); }
public void SetValue(object objEntity, object dbValue) { if (Convert.IsDBNull(dbValue)) { return; } if (_PropertyInfo != null) { _PropertyInfo.SetValue(objEntity, /* Convert.ChangeType */ DBConvert.ChangeType(dbValue, _DataType), null); } else if (_FieldInfo != null) { _FieldInfo.SetValue(objEntity, /* Convert.ChangeType */ DBConvert.ChangeType(dbValue, _DataType)); } }
protected override object GetParameterValue(IProviderType type, object value) { if (value == null) { return(DBNull.Value); } var type2 = value.GetType(); var closestRuntimeType = type.GetClosestRuntimeType(); if (closestRuntimeType == typeof(OracleNumber)) { return(new OracleNumber(Convert.ToDecimal(value))); } return(closestRuntimeType == type2 ? value : DBConvert.ChangeType(value, closestRuntimeType)); }
public override object GetParameterValue(int parameterIndex) { if (((parameters == null) || (parameterIndex < 0)) || (parameterIndex > parameters.Count)) { throw Error.ArgumentOutOfRange("parameterIndex"); } if ((session != null) && !session.IsBuffered) { session.Buffer(); } SqlParameterInfo info = parameters[parameterIndex]; object obj2 = command.Parameters[parameterIndex].Value; if (obj2 == DBNull.Value) { obj2 = null; } if (obj2 != null) { var objType = obj2.GetType(); var ns = objType.Namespace; if (ns == "Oracle.DataAccess.Types") { if (((INullable)obj2).IsNull) { return(null); } var propertyInfo = objType.GetProperty("Value"); if (propertyInfo != null) { obj2 = propertyInfo.GetValue(obj2, null); if (obj2 != null) { objType = obj2.GetType(); } } } if (objType != info.Parameter.ClrType) { return(DBConvert.ChangeType(obj2, info.Parameter.ClrType)); } } return(obj2); }
internal static T TryConvert <T>(object dbValue) { if (dbValue == null || Convert.IsDBNull(dbValue)) { return(default(T)); } else { try { return((T)dbValue); } catch (InvalidCastException) { return(DBConvert.ChangeType <T>(dbValue)); // (T)Convert.ChangeType(dbValue, typeof(T).TryUnderlyingType()); } } }
private MetaType InitInheritedType(TypeMapping typeMap, MappedType type) { derivedTypes.Add(type.Type, type); if (typeMap.InheritanceCode != null) { if (Discriminator == null) { throw Error.NoDiscriminatorFound(type.Name); } if (type.Type.IsAbstract) { throw Error.AbstractClassAssignInheritanceDiscriminator(type.Type); } object objB = DBConvert.ChangeType(typeMap.InheritanceCode, Discriminator.Type); foreach (object obj3 in inheritanceCodes.Keys) { if ((((objB.GetType() == typeof(string)) && (((string)objB).Trim().Length == 0)) && ((obj3.GetType() == typeof(string)) && (((string)obj3).Trim().Length == 0))) || Equals(obj3, objB)) { throw Error.InheritanceCodeUsedForMultipleTypes(objB); } } if (type.inheritanceCode != null) { throw Error.InheritanceTypeHasMultipleDiscriminators(type); } type.inheritanceCode = objB; inheritanceCodes.Add(objB, type); if (typeMap.IsInheritanceDefault) { if (inheritanceDefault != null) { throw Error.InheritanceTypeHasMultipleDefaults(type); } inheritanceDefault = type; } } foreach (TypeMapping mapping in typeMap.DerivedTypes) { InitDerivedTypes(mapping); } return(type); }
protected object GetParameterValue(SqlType type, object value) { if (value == null) { return(DBNull.Value); } else { Type vType = value.GetType(); Type pType = type.GetClosestRuntimeType(); if (pType == vType) { return(value); } else { return(DBConvert.ChangeType(value, pType)); } } }
private void CoerceToFirst(SqlExpression arg1, ref SqlExpression arg2) { if ((arg1.SqlType != null) && (arg2.SqlType != null)) { if (arg2.NodeType == SqlNodeType.Value) { var value2 = (SqlValue)arg2; arg2 = sql.Value(arg1.ClrType, arg1.SqlType, DBConvert.ChangeType(value2.Value, arg1.ClrType), value2.IsClientSpecified, arg2.SourceExpression); } else if ((arg2.NodeType == SqlNodeType.ClientParameter) && (arg2.SqlType != arg1.SqlType)) { ((SqlClientParameter)arg2).SetSqlType(arg1.SqlType); } else { arg2 = SqlFactory.UnaryConvert(arg1.ClrType, arg1.SqlType, arg2, arg2.SourceExpression); } } }
public virtual object GetParameterValue(int parameterIndex) { if (((parameters == null) || (parameterIndex < 0)) || (parameterIndex > parameters.Count)) { throw Error.ArgumentOutOfRange("parameterIndex"); } if ((session != null) && !session.IsBuffered) { session.Buffer(); } SqlParameterInfo info = parameters[parameterIndex]; object obj2 = command.Parameters[parameterIndex].Value; if (obj2 == DBNull.Value) { obj2 = null; } if ((obj2 != null) && (obj2.GetType() != info.Parameter.ClrType)) { return(DBConvert.ChangeType(obj2, info.Parameter.ClrType)); } return(obj2); }
private void CoerceToFirst(SqlExpression arg1, ref SqlExpression arg2) { if (arg1.SqlType != null && arg2.SqlType != null) { if (arg2.NodeType == SqlNodeType.Value) { SqlValue val = (SqlValue)arg2; arg2 = sql.Value( arg1.ClrType, arg1.SqlType, DBConvert.ChangeType(val.Value, arg1.ClrType), val.IsClientSpecified, arg2.SourceExpression ); } else if (arg2.NodeType == SqlNodeType.ClientParameter && arg2.SqlType != arg1.SqlType) { SqlClientParameter cp = (SqlClientParameter)arg2; cp.SetSqlType(arg1.SqlType); } else { arg2 = sql.UnaryConvert(arg1.ClrType, arg1.SqlType, arg2, arg2.SourceExpression); } } }
private void CoerceBinaryArgs(ref SqlExpression arg1, ref SqlExpression arg2) { if ((arg1.SqlType != null) && (arg2.SqlType != null)) { if (!arg1.SqlType.IsSameTypeFamily(arg2.SqlType)) { if ((arg1.ClrType != typeof(bool)) && (arg2.ClrType != typeof(bool))) { if (arg2.NodeType == SqlNodeType.Value) { var value2 = (SqlValue)arg2; object obj2 = value2.Value; if (!value2.ClrType.IsAssignableFrom(arg1.ClrType)) { obj2 = DBConvert.ChangeType(obj2, arg1.ClrType); } IProviderType sqlType = this.typeProvider.ChangeTypeFamilyTo(arg2.SqlType, arg1.SqlType); arg2 = this.sql.Value(arg1.ClrType, sqlType, obj2, value2.IsClientSpecified, arg2.SourceExpression); } else if (arg1.NodeType == SqlNodeType.Value) { var value3 = (SqlValue)arg1; object obj3 = value3.Value; if (!value3.ClrType.IsAssignableFrom(arg2.ClrType)) { obj3 = DBConvert.ChangeType(obj3, arg2.ClrType); } IProviderType type2 = this.typeProvider.ChangeTypeFamilyTo(arg1.SqlType, arg2.SqlType); arg1 = this.sql.Value(arg2.ClrType, type2, obj3, value3.IsClientSpecified, arg1.SourceExpression); } else if ((arg2.NodeType == SqlNodeType.ClientParameter) && (arg2.SqlType != arg1.SqlType)) { ((SqlClientParameter)arg2).SetSqlType(arg1.SqlType); } else if ((arg1.NodeType == SqlNodeType.ClientParameter) && (arg1.SqlType != arg2.SqlType)) { ((SqlClientParameter)arg1).SetSqlType(arg2.SqlType); } else { int num = arg1.SqlType.ComparePrecedenceTo(arg2.SqlType); if (num > 0) { arg2 = SqlFactory.UnaryConvert(arg1.ClrType, arg1.SqlType, arg2, arg2.SourceExpression); } else if (num < 0) { arg1 = SqlFactory.UnaryConvert(arg2.ClrType, arg2.SqlType, arg1, arg1.SourceExpression); } } } } else if ((arg1.SqlType.HasPrecisionAndScale && arg2.SqlType.HasPrecisionAndScale) && (arg1.SqlType != arg2.SqlType)) { IProviderType bestType = this.typeProvider.GetBestType(arg1.SqlType, arg2.SqlType); var expression = arg1 as SqlSimpleTypeExpression; if (expression != null) { expression.SetSqlType(bestType); } var expression2 = arg2 as SqlSimpleTypeExpression; if (expression2 != null) { expression2.SetSqlType(bestType); } } } }
// Methods internal AttributedRootType(AttributedMetaModel model, AttributedMetaTable table, Type type) : base(model, table, type, null) { this.model = model; var customAttributes = model.AttributeProvider.GetInheritanceMappingAttribute(type); //(InheritanceMappingAttribute[])type.GetCustomAttributes(typeof(InheritanceMappingAttribute), true); if (customAttributes != null && customAttributes.Length > 0) { if (this.Discriminator == null) { throw Mapping.Error.NoDiscriminatorFound(type); } if (!MappingSystem.IsSupportedDiscriminatorType(this.Discriminator.Type)) { throw Mapping.Error.DiscriminatorClrTypeNotSupported(this.Discriminator.DeclaringType.Name, this.Discriminator.Name, this.Discriminator.Type); } this.types = new Dictionary <Type, MetaType>(); this.types.Add(type, this); this.codeMap = new Dictionary <object, MetaType>(); foreach (InheritanceMappingAttribute attribute in customAttributes) { if (!type.IsAssignableFrom(attribute.Type)) { throw Mapping.Error.InheritanceTypeDoesNotDeriveFromRoot(attribute.Type, type); } if (attribute.Type.IsAbstract) { throw Mapping.Error.AbstractClassAssignInheritanceDiscriminator(attribute.Type); } AttributedMetaType type2 = this.CreateInheritedType(type, attribute.Type); if (attribute.Code == null) { throw Mapping.Error.InheritanceCodeMayNotBeNull(); } if (type2.inheritanceCode != null) { throw Mapping.Error.InheritanceTypeHasMultipleDiscriminators(attribute.Type); } object objB = DBConvert.ChangeType(attribute.Code, this.Discriminator.Type); foreach (object obj3 in this.codeMap.Keys) { if ((((objB.GetType() == typeof(string)) && (((string)objB).Trim().Length == 0)) && ((obj3.GetType() == typeof(string)) && (((string)obj3).Trim().Length == 0))) || object.Equals(obj3, objB)) { throw Mapping.Error.InheritanceCodeUsedForMultipleTypes(objB); } } type2.inheritanceCode = objB; this.codeMap.Add(objB, type2); if (attribute.IsDefault) { if (this.inheritanceDefault != null) { throw Mapping.Error.InheritanceTypeHasMultipleDefaults(type); } this.inheritanceDefault = type2; } } if (this.inheritanceDefault == null) { throw Mapping.Error.InheritanceHierarchyDoesNotDefineDefault(type); } } if (this.types != null) { this.inheritanceTypes = this.types.Values.ToList <MetaType>().AsReadOnly(); } else { this.inheritanceTypes = new MetaType[] { this }.ToList <MetaType>().AsReadOnly(); } this.Validate(); }
internal static object ReadValue(this XElement xe, BindableDynamicObject.XmlSettings xmlSettings) { if ((bool?)xe.Attribute(XnNil) ?? false) { return(null); } string declaredType = null; switch (xmlSettings.TypeSchema) { case BindableDynamicObject.XmlSettings.DataTypeSchema.Xsd: declaredType = xe.GetXsdTypeAttributeString(); if (string.IsNullOrEmpty(declaredType)) { if (xmlSettings.IsImplicit()) { declaredType = xe.GetNetTypeAttributeString(); } } else { try { return(_XsdDataContractSerializer.ReadObject(xe.CreateReader(), false)); } catch { } } break; case BindableDynamicObject.XmlSettings.DataTypeSchema.Net: declaredType = xe.GetNetTypeAttributeString(); if (declaredType == null && xmlSettings.IsImplicit()) { declaredType = xe.GetXsdTypeAttributeString(); } break; default: if (xmlSettings.IsImplicit()) { declaredType = xe.GetXsdTypeAttributeString(); if (string.IsNullOrEmpty(declaredType)) { declaredType = xe.GetNetTypeAttributeString(); } else { try { return(_XsdDataContractSerializer.ReadObject(xe.CreateReader(), false)); } catch { } } } break; } Type valueType = GetXsdType(declaredType); if (valueType == typeof(string)) { return(xe.Value); } try { return(DBConvert.ChangeType(xe.Value, valueType)); } catch { return(xe.Value); } }
internal AttributedRootType(AttributedMetaModel model, AttributedMetaTable table, Type type) : base(model, table, type, null) { // check for inheritance and create all other types var inheritanceAttributes = GetInheritanceMappingAttributes(type, model); if (inheritanceAttributes.Count > 0) { if (Discriminator == null) { throw Error.NoDiscriminatorFound(type); } if (!MappingSystem.IsSupportedDiscriminatorType(Discriminator.Type)) { throw Error.DiscriminatorClrTypeNotSupported( Discriminator.DeclaringType.Name, Discriminator.Name, Discriminator.Type); } types = new Dictionary <Type, MetaType>(); types.Add(type, this); // add self var codeMap = new Dictionary <object, MetaType>(); // initialize inheritance types foreach (var inheritanceAttribute in inheritanceAttributes) { if (!type.IsAssignableFrom(inheritanceAttribute.Type)) { throw Error.InheritanceTypeDoesNotDeriveFromRoot(inheritanceAttribute.Type, type); } if (inheritanceAttribute.Type.IsAbstract) { throw Error.AbstractClassAssignInheritanceDiscriminator(inheritanceAttribute.Type); } var inheritedType = CreateInheritedType(type, inheritanceAttribute.Type); if (inheritanceAttribute.Code == null) { throw Error.InheritanceCodeMayNotBeNull(); } if (inheritedType.inheritanceCode != null) { throw Error.InheritanceTypeHasMultipleDiscriminators(inheritanceAttribute.Type); } var codeValue = DBConvert.ChangeType(inheritanceAttribute.Code, Discriminator.Type); foreach (var codeMapKey in codeMap.Keys) { // if the keys are equal, or if they are both strings containing only spaces // they are considered equal if ((codeValue is string && ((string)codeValue).Trim().Length == 0 && codeMapKey is string && ((string)codeMapKey).Trim().Length == 0) || Equals(codeMapKey, codeValue)) { throw Error.InheritanceCodeUsedForMultipleTypes(codeValue); } } inheritedType.inheritanceCode = codeValue; codeMap.Add(codeValue, inheritedType); if (inheritanceAttribute.IsDefault) { if (inheritanceDefault != null) { throw Error.InheritanceTypeHasMultipleDefaults(type); } inheritanceDefault = inheritedType; } } if (inheritanceDefault == null) { throw Error.InheritanceHierarchyDoesNotDefineDefault(type); } } inheritanceTypes = types == null ? new MetaType[] { this } .ToList() .AsReadOnly() : types.Values.ToList().AsReadOnly(); Validate(); }
internal AttributedRootType(AttributedMetaModel model, AttributedMetaTable table, Type type) : base(model, table, type, null) { // check for inheritance and create all other types InheritanceMappingAttribute[] inheritanceInfo = (InheritanceMappingAttribute[])type.GetCustomAttributes(typeof(InheritanceMappingAttribute), true); if (inheritanceInfo.Length > 0) { if (this.Discriminator == null) { throw Error.NoDiscriminatorFound(type); } if (!MappingSystem.IsSupportedDiscriminatorType(this.Discriminator.Type)) { throw Error.DiscriminatorClrTypeNotSupported(this.Discriminator.DeclaringType.Name, this.Discriminator.Name, this.Discriminator.Type); } this.types = new Dictionary <Type, MetaType>(); this.types.Add(type, this); // add self this.codeMap = new Dictionary <object, MetaType>(); // initialize inheritance types foreach (InheritanceMappingAttribute attr in inheritanceInfo) { if (!type.IsAssignableFrom(attr.Type)) { throw Error.InheritanceTypeDoesNotDeriveFromRoot(attr.Type, type); } if (attr.Type.IsAbstract) { throw Error.AbstractClassAssignInheritanceDiscriminator(attr.Type); } AttributedMetaType mt = this.CreateInheritedType(type, attr.Type); if (attr.Code == null) { throw Error.InheritanceCodeMayNotBeNull(); } if (mt.inheritanceCode != null) { throw Error.InheritanceTypeHasMultipleDiscriminators(attr.Type); } object codeValue = DBConvert.ChangeType(attr.Code, this.Discriminator.Type); foreach (object d in codeMap.Keys) { // if the keys are equal, or if they are both strings containing only spaces // they are considered equal if ((codeValue.GetType() == typeof(string) && ((string)codeValue).Trim().Length == 0 && d.GetType() == typeof(string) && ((string)d).Trim().Length == 0) || object.Equals(d, codeValue)) { throw Error.InheritanceCodeUsedForMultipleTypes(codeValue); } } mt.inheritanceCode = codeValue; this.codeMap.Add(codeValue, mt); if (attr.IsDefault) { if (this.inheritanceDefault != null) { throw Error.InheritanceTypeHasMultipleDefaults(type); } this.inheritanceDefault = mt; } } if (this.inheritanceDefault == null) { throw Error.InheritanceHierarchyDoesNotDefineDefault(type); } } if (this.types != null) { this.inheritanceTypes = this.types.Values.ToList().AsReadOnly(); } else { this.inheritanceTypes = new MetaType[] { this }.ToList().AsReadOnly(); } this.Validate(); }