public override void Test() { var connstr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString; var conn = new SqlConnection(connstr); try { conn.Open(); using (SqlCommand command = new SqlCommand("select * from dbo.FilmTbl where filmcode=42690", conn)) using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { var f = new Film(); f.FilmCode = SafeClrConvert.ToInt32(reader["FilmCode"]); f.FilmDesc = SafeClrConvert.ToString(reader["FilmDesc"]); f.Iranian = SafeClrConvert.ToBoolean(reader["Iranian"]); f.Director = SafeClrConvert.ToString(reader["Director"]); f.Summary = SafeClrConvert.ToString(reader["Summary"]); f.ReleaseDate = SafeClrConvert.ToString(reader["ReleaseDate"]); f.RunningTime = SafeClrConvert.ToString(reader["RunningTime"]); f.Genre = SafeClrConvert.ToString(reader["Genre"]); f.Trailer = SafeClrConvert.ToString(reader["Trailer"]); f.Year = SafeClrConvert.ToString(reader["Year"]); f.Producer = SafeClrConvert.ToString(reader["Producer"]); f.Casting = SafeClrConvert.ToString(reader["Casting"]); f.Credits = SafeClrConvert.ToString(reader["Credits"]); f.Filmimage = SafeClrConvert.ToString(reader["Filmimage"]); f.Film_id = SafeClrConvert.ToInt32(reader["Film_id"]); f.Category_Id = SafeClrConvert.ToInt32(reader["Category_Id"]); f.date = SafeClrConvert.ToDateTime(reader["date"]); f.FilmHorizontalImage = SafeClrConvert.ToString(reader["FilmHorizontalImage"]); f.distribution = SafeClrConvert.ToString(reader["distribution"]); f.ShowSale = SafeClrConvert.ToBoolean(reader["ShowSale"]); f.Film_Field1 = SafeClrConvert.ToString(reader["Film_Field1"]); f.Film_Field2 = SafeClrConvert.ToString(reader["Film_Field2"]); f.Film_Order = SafeClrConvert.ToInt32(reader["Film_Order"]); f.Rating = SafeClrConvert.ToInt32(reader["Rating"]); f.Rating_Users = SafeClrConvert.ToInt32(reader["Rating_Users"]); f.Film_Field3 = SafeClrConvert.ToString(reader["Film_Field3"]); System.Console.WriteLine(JsonConvert.SerializeObject(f)); } } } catch (Exception e) { System.Console.WriteLine(e); } }
protected virtual LogItem Transform(IDataReader reader) { var result = new LogItem { Row = SafeClrConvert.ToLong(reader["Row"]), Id = SafeClrConvert.ToInt(reader["Id"]), Member = SafeClrConvert.ToString(reader["Member"]), Category = SafeClrConvert.ToString(reader["Category"]), Message = SafeClrConvert.ToString(reader["Message"]), FilePath = SafeClrConvert.ToString(reader["FilePath"]), LogDate = SafeClrConvert.ToDateTime(reader["LogDate"]), Line = SafeClrConvert.ToInt(reader["Line"]), }; return(result); }
protected override void OnBasicPropertyDetected(string propertyName, string propertyValue, JsonValueType itemType) { if (string.Compare(propertyName, "api", StringComparison.OrdinalIgnoreCase) == 0) { Api = propertyValue; return; } if (string.Compare(propertyName, "date", StringComparison.OrdinalIgnoreCase) == 0) { Date = SafeClrConvert.ToDateTime(propertyValue); return; } if (string.Compare(propertyName, "lang", StringComparison.OrdinalIgnoreCase) == 0) { Lang = propertyValue; return; } if (string.Compare(propertyName, "data", StringComparison.OrdinalIgnoreCase) == 0) { Data = propertyValue; return; } }
public static T AppSetting <T>(string key, T defaultValue = default(T)) where T : struct { var result = default(T); var type = typeof(T); var value = ConfigurationManager.AppSettings[key]; if (type.IsEnum) { if (value.IsNumeric()) { result = SafeClrConvert.ToInt(value).ToEnum(defaultValue); } else { if (!Enum.TryParse(value, out result)) { result = defaultValue; } } } else { if (type.IsBasicType()) { do { if (type == TypeHelper.TypeOfBool) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToBoolean(value), type); break; } if (type == TypeHelper.TypeOfInt16) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToInt16(value), type); break; } if (type == TypeHelper.TypeOfInt32) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToInt32(value), type); break; } if (type == TypeHelper.TypeOfInt64) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToInt64(value), type); break; } if (type == TypeHelper.TypeOfByte) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToByte(value), type); break; } if (type == TypeHelper.TypeOfSByte) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToSByte(value), type); break; } if (type == TypeHelper.TypeOfUInt16) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToUInt16(value), type); break; } if (type == TypeHelper.TypeOfUInt32) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToUInt32(value), type); break; } if (type == TypeHelper.TypeOfUInt64) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToUInt64(value), type); break; } if (type == TypeHelper.TypeOfChar) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToChar(value), type); break; } if (type == TypeHelper.TypeOfDateTime) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToDateTime(value), type); break; } if (type == TypeHelper.TypeOfDecimal) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToDecimal(value), type); break; } if (type == TypeHelper.TypeOfDouble) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToDouble(value), type); break; } if (type == TypeHelper.TypeOfFloat) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToSingle(value), type); break; } if (type == TypeHelper.TypeOfGuid) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToGuid(value), type); break; } if (type == TypeHelper.TypeOfTimeSpan) { result = (T)System.Convert.ChangeType(SafeClrConvert.ToTimeSpan(value), type); break; } if (type == TypeHelper.TypeOfString) { result = (T)((object)value); break; } }while (false); } } return(result); }
public static T ReflectionSafeTransform <T>(IDataReader reader) where T : class, new() { var result = new T(); if (!reader.IsClosed) { var props = GlobalReflectionPropertyCache.Cache.GetPublicInstanceReadableProperties <T>(); for (var i = 0; i < reader.FieldCount; i++) { if (!reader.IsDBNull(i)) { var column = reader.GetName(i); var value = reader[i]; var prop = props.FirstOrDefault(p => string.Compare(p.Name, column, true) == 0); if (prop != null) { try { if (prop.PropertyType.IsNullable()) { if (value != null) { object nullableValue = null; object convertedValue = null; var nullableFinalType = prop.PropertyType.GenericTypeArguments[0]; if (nullableFinalType == TypeHelper.TypeOfByte) { convertedValue = SafeClrConvert.ToByte(value); } else if (nullableFinalType == TypeHelper.TypeOfInt16) { convertedValue = SafeClrConvert.ToInt16(value); } else if (nullableFinalType == TypeHelper.TypeOfInt32) { convertedValue = SafeClrConvert.ToInt32(value); } else if (nullableFinalType == TypeHelper.TypeOfInt64) { convertedValue = SafeClrConvert.ToInt64(value); } else if (nullableFinalType == TypeHelper.TypeOfSByte) { convertedValue = SafeClrConvert.ToSByte(value); } else if (nullableFinalType == TypeHelper.TypeOfUInt16) { convertedValue = SafeClrConvert.ToUInt16(value); } else if (nullableFinalType == TypeHelper.TypeOfUInt32) { convertedValue = SafeClrConvert.ToUInt32(value); } else if (nullableFinalType == TypeHelper.TypeOfUInt64) { convertedValue = SafeClrConvert.ToUInt64(value); } else if (nullableFinalType == TypeHelper.TypeOfSingle) { convertedValue = SafeClrConvert.ToSingle(value); } else if (nullableFinalType == TypeHelper.TypeOfDouble) { convertedValue = SafeClrConvert.ToDouble(value); } else if (nullableFinalType == TypeHelper.TypeOfDecimal) { convertedValue = SafeClrConvert.ToDecimal(value); } else if (nullableFinalType == TypeHelper.TypeOfString) { convertedValue = SafeClrConvert.ToString(value); } else if (nullableFinalType == TypeHelper.TypeOfDateTime) { convertedValue = SafeClrConvert.ToDateTime(value); } else if (nullableFinalType == TypeHelper.TypeOfBool) { convertedValue = SafeClrConvert.ToBoolean(value); } nullableValue = Activator.CreateInstance(prop.PropertyType, new object[] { convertedValue }); prop.SetValue(result, nullableValue); } } else { object convertedValue = null; var propType = prop.PropertyType; if (propType == TypeHelper.TypeOfByte) { convertedValue = SafeClrConvert.ToByte(value); } else if (propType == TypeHelper.TypeOfInt16) { convertedValue = SafeClrConvert.ToInt16(value); } else if (propType == TypeHelper.TypeOfInt32) { convertedValue = SafeClrConvert.ToInt32(value); } else if (propType == TypeHelper.TypeOfInt64) { convertedValue = SafeClrConvert.ToInt64(value); } else if (propType == TypeHelper.TypeOfSByte) { convertedValue = SafeClrConvert.ToSByte(value); } else if (propType == TypeHelper.TypeOfUInt16) { convertedValue = SafeClrConvert.ToUInt16(value); } else if (propType == TypeHelper.TypeOfUInt32) { convertedValue = SafeClrConvert.ToUInt32(value); } else if (propType == TypeHelper.TypeOfUInt64) { convertedValue = SafeClrConvert.ToUInt64(value); } else if (propType == TypeHelper.TypeOfSingle) { convertedValue = SafeClrConvert.ToSingle(value); } else if (propType == TypeHelper.TypeOfDouble) { convertedValue = SafeClrConvert.ToDouble(value); } else if (propType == TypeHelper.TypeOfDecimal) { convertedValue = SafeClrConvert.ToDecimal(value); } else if (propType == TypeHelper.TypeOfString) { convertedValue = SafeClrConvert.ToString(value); } else if (propType == TypeHelper.TypeOfDateTime) { convertedValue = SafeClrConvert.ToDateTime(value); } else if (propType == TypeHelper.TypeOfBool) { convertedValue = SafeClrConvert.ToBoolean(value); } if (convertedValue != null) { prop.SetValue(result, convertedValue); } else { prop.SetValue(result, value); } } } catch (Exception e) { throw new Exception($"error reading column {column} into prop {prop.Name}", e); } } } } } return(result); }
public static void SafeApplyOutputs(this DbCommand cmd, object parameters, PropertyInfo[] properties = null) { cmd.ApplyOutputs(parameters, (obj, prop, value) => { if (value == null || DBNull.Value.Equals(value)) { if (prop.PropertyType.IsNullable() && prop.CanWrite) { prop.SetValue(obj, null); } } else { if (prop.PropertyType == typeof(CommandParameter)) { var cp = prop.GetValue(obj) as CommandParameter; if (cp != null) { cp.Value = value; } } else { do { if (prop.PropertyType == TypeHelper.TypeOfInt16 || prop.PropertyType == TypeHelper.TypeOfNullableInt16) { prop.SetValue(obj, SafeClrConvert.ToInt16(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfInt32 || prop.PropertyType == TypeHelper.TypeOfNullableInt32) { prop.SetValue(obj, SafeClrConvert.ToInt32(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfInt64 || prop.PropertyType == TypeHelper.TypeOfNullableInt64) { prop.SetValue(obj, SafeClrConvert.ToInt64(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfUInt16 || prop.PropertyType == TypeHelper.TypeOfNullableUInt16) { prop.SetValue(obj, SafeClrConvert.ToUInt16(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfUInt32 || prop.PropertyType == TypeHelper.TypeOfNullableUInt32) { prop.SetValue(obj, SafeClrConvert.ToUInt32(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfUInt64 || prop.PropertyType == TypeHelper.TypeOfNullableUInt64) { prop.SetValue(obj, SafeClrConvert.ToUInt64(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfSingle || prop.PropertyType == TypeHelper.TypeOfNullableSingle) { prop.SetValue(obj, SafeClrConvert.ToSingle(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfFloat || prop.PropertyType == TypeHelper.TypeOfNullableFloat) { prop.SetValue(obj, SafeClrConvert.ToFloat(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfDouble || prop.PropertyType == TypeHelper.TypeOfNullableDouble) { prop.SetValue(obj, SafeClrConvert.ToDouble(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfDecimal || prop.PropertyType == TypeHelper.TypeOfNullableDecimal) { prop.SetValue(obj, SafeClrConvert.ToDecimal(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfByte || prop.PropertyType == TypeHelper.TypeOfNullableByte) { prop.SetValue(obj, SafeClrConvert.ToByte(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfSByte || prop.PropertyType == TypeHelper.TypeOfNullableSByte) { prop.SetValue(obj, SafeClrConvert.ToSByte(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfChar || prop.PropertyType == TypeHelper.TypeOfNullableChar) { prop.SetValue(obj, SafeClrConvert.ToChar(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfString) { prop.SetValue(obj, SafeClrConvert.ToString(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfBool || prop.PropertyType == TypeHelper.TypeOfNullableBool) { prop.SetValue(obj, SafeClrConvert.ToBoolean(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfDateTime || prop.PropertyType == TypeHelper.TypeOfNullableDateTime) { prop.SetValue(obj, SafeClrConvert.ToDateTime(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfDateTimeOffset || prop.PropertyType == TypeHelper.TypeOfNullableDateTimeOffset) { prop.SetValue(obj, SafeClrConvert.ToDateTime(value)); break; } if (prop.PropertyType == TypeHelper.TypeOfTimeSpan || prop.PropertyType == TypeHelper.TypeOfNullableTimeSpan) { prop.SetValue(obj, SafeClrConvert.ToTimeSpan(value)); break; } prop.SetValue(obj, value); }while (false); } } }, properties); }
public object Map(IDataReader reader, Type type) { var result = Activator.CreateInstance(type); ReflectionHelper.ForEachPublicInstanceReadableProperty(type, prop => { if (reader.HasColumn(prop.Name)) { var value = reader[prop.Name]; if (value != null && !DBNull.Value.Equals(value)) { var propType = prop.PropertyType; if (propType == TypeHelper.TypeOfBool || propType == TypeHelper.TypeOfNullableBool) { prop.SetValue(result, SafeClrConvert.ToBoolean(value)); } else if (propType == TypeHelper.TypeOfByte || propType == TypeHelper.TypeOfNullableByte) { prop.SetValue(result, SafeClrConvert.ToByte(value)); } else if (propType == TypeHelper.TypeOfByteArray) { prop.SetValue(result, (byte[])value); } else if (propType == TypeHelper.TypeOfChar || propType == TypeHelper.TypeOfNullableChar) { prop.SetValue(result, SafeClrConvert.ToChar(value)); } else if (propType == TypeHelper.TypeOfDateTime || propType == TypeHelper.TypeOfNullableDateTime) { prop.SetValue(result, SafeClrConvert.ToDateTime(value)); } else if (propType == TypeHelper.TypeOfDateTimeOffset || propType == TypeHelper.TypeOfNullableDateTimeOffset) { prop.SetValue(result, SafeClrConvert.ToDateTime(value)); } else if (propType == TypeHelper.TypeOfDecimal || propType == TypeHelper.TypeOfNullableDecimal) { prop.SetValue(result, SafeClrConvert.ToDecimal(value)); } else if (propType == TypeHelper.TypeOfDouble || propType == TypeHelper.TypeOfNullableDouble) { prop.SetValue(result, SafeClrConvert.ToDouble(value)); } else if (propType == TypeHelper.TypeOfFloat || propType == TypeHelper.TypeOfNullableFloat) { prop.SetValue(result, SafeClrConvert.ToSingle(value)); } else if (propType == TypeHelper.TypeOfGuid) { prop.SetValue(result, new Guid(SafeClrConvert.ToString(value))); } else if (propType == TypeHelper.TypeOfInt16 || propType == TypeHelper.TypeOfNullableInt16) { prop.SetValue(result, SafeClrConvert.ToInt16(value)); } else if (propType == TypeHelper.TypeOfInt32 || propType == TypeHelper.TypeOfNullableInt32) { prop.SetValue(result, SafeClrConvert.ToInt32(value)); } else if (propType == TypeHelper.TypeOfInt64 || propType == TypeHelper.TypeOfNullableInt64) { prop.SetValue(result, SafeClrConvert.ToInt64(value)); } else if (propType == TypeHelper.TypeOfSByte || propType == TypeHelper.TypeOfNullableSByte) { prop.SetValue(result, SafeClrConvert.ToSByte(value)); } else if (propType == TypeHelper.TypeOfString) { prop.SetValue(result, SafeClrConvert.ToString(value)); } else if (propType == TypeHelper.TypeOfTimeSpan || propType == TypeHelper.TypeOfNullableTimeSpan) { prop.SetValue(result, SafeClrConvert.ToDateTime(value)); } else if (propType == TypeHelper.TypeOfUInt16 || propType == TypeHelper.TypeOfNullableUInt16) { prop.SetValue(result, SafeClrConvert.ToUInt16(value)); } else if (propType == TypeHelper.TypeOfUInt32 || propType == TypeHelper.TypeOfNullableUInt32) { prop.SetValue(result, SafeClrConvert.ToUInt32(value)); } else if (propType == TypeHelper.TypeOfUInt64 || propType == TypeHelper.TypeOfNullableUInt64) { prop.SetValue(result, SafeClrConvert.ToUInt64(value)); } } } }); return(result); }
public static System.DateTime ToDateTime(this string s, System.DateTime @default = default(System.DateTime)) { return(SafeClrConvert.ToDateTime(s, @default)); }
protected override void SetProperty(SetPropertyArgs args) { switch (args.PropertyName) { case "FilmCode": FilmCode = SafeClrConvert.ToInt32(args.GivenValue); break; case "FilmDesc": FilmDesc = SafeClrConvert.ToString(args.GivenValue); break; case "Iranian": Iranian = SafeClrConvert.ToBoolean(args.GivenValue); break; case "Director": Director = SafeClrConvert.ToString(args.GivenValue); break; case "Summary": Summary = SafeClrConvert.ToString(args.GivenValue); break; case "ReleaseDate": ReleaseDate = SafeClrConvert.ToString(args.GivenValue); break; case "RunningTime": RunningTime = SafeClrConvert.ToString(args.GivenValue); break; case "Genre": Genre = SafeClrConvert.ToString(args.GivenValue); break; case "Trailer": Trailer = SafeClrConvert.ToString(args.GivenValue); break; case "Year": Year = SafeClrConvert.ToString(args.GivenValue); break; case "Producer": Producer = SafeClrConvert.ToString(args.GivenValue); break; case "Casting": Casting = SafeClrConvert.ToString(args.GivenValue); break; case "Credits": Credits = SafeClrConvert.ToString(args.GivenValue); break; case "Filmimage": Filmimage = SafeClrConvert.ToString(args.GivenValue); break; case "Film_id": Film_id = SafeClrConvert.ToInt32(args.GivenValue); break; case "Category_Id": Category_Id = SafeClrConvert.ToInt32(args.GivenValue); break; case "date": date = SafeClrConvert.ToDateTime(args.GivenValue); break; case "FilmHorizontalImage": FilmHorizontalImage = SafeClrConvert.ToString(args.GivenValue); break; case "distribution": distribution = SafeClrConvert.ToString(args.GivenValue); break; case "ShowSale": ShowSale = SafeClrConvert.ToBoolean(args.GivenValue); break; case "Film_Field1": Film_Field1 = SafeClrConvert.ToString(args.GivenValue); break; case "Film_Field2": Film_Field2 = SafeClrConvert.ToString(args.GivenValue); break; case "Film_Order": Film_Order = SafeClrConvert.ToInt32(args.GivenValue); break; case "Rating": Rating = SafeClrConvert.ToInt32(args.GivenValue); break; case "Rating_Users": Rating_Users = SafeClrConvert.ToInt32(args.GivenValue); break; case "Film_Field3": Film_Field3 = SafeClrConvert.ToString(args.GivenValue); break; } }