public bool IsPrivate() { // http://en.wikipedia.org/wiki/Private_network // Private IP Addresses are: // 24-bit block: 10.0.0.0 through 10.255.255.255 // 20-bit block: 172.16.0.0 through 172.31.255.255 // 16-bit block: 192.168.0.0 through 192.168.255.255 // Link-local addresses: 169.254.0.0 through 169.254.255.255 (http://en.wikipedia.org/wiki/Link-local_address) var is24BitBlock = A == "10"; if (is24BitBlock) { return(true); // Return to prevent further processing } var b = SafeClrConvert.ToByte(B); var is20BitBlock = A == "172" && B != "*" && b >= 16 && b <= 31; if (is20BitBlock) { return(true); // Return to prevent further processing } var is16BitBlock = A == "192" && B == "168"; if (is16BitBlock) { return(true); // Return to prevent further processing } var isLinkLocalAddress = A == "169" && B == "254"; return(isLinkLocalAddress); }
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 void Parse(string version) { if (!string.IsNullOrEmpty(version)) { var firstDotindex = version.IndexOf("."); var dashIndex = -1; var lastPart = ""; var alphaIndex = -1; var hasAlpha = false; if (firstDotindex > 0) { MajorVersion = version.Substring(0, firstDotindex); var secondDotindex = version.IndexOf(".", firstDotindex + 1); if (secondDotindex > 0) { MinorVersion = version.Substring(firstDotindex + 1, secondDotindex - firstDotindex - 1); var revisionrpart = version.Substring(secondDotindex + 1); var thirdDotIndex = revisionrpart.IndexOf('.'); if (thirdDotIndex >= 0) { RevisionNumber = revisionrpart.Substring(0, thirdDotIndex); var buildPart = revisionrpart.Substring(thirdDotIndex + 1); if (buildPart.Length > 0) { dashIndex = buildPart.IndexOf('-'); if (dashIndex >= 0) { BuildNumber = buildPart.Substring(0, dashIndex); lastPart = buildPart.Substring(dashIndex + 1).ToLower(); } else { foreach (var ch in buildPart) { alphaIndex++; if (Char.IsLetter(ch)) { hasAlpha = true; break; } } if (hasAlpha) { BuildNumber = buildPart.Substring(0, alphaIndex); lastPart = buildPart.Substring(alphaIndex).ToLower(); } else { BuildNumber = buildPart; } } } } else { dashIndex = revisionrpart.IndexOf('-'); if (dashIndex >= 0) { RevisionNumber = revisionrpart.Substring(0, dashIndex); lastPart = revisionrpart.Substring(dashIndex + 1).ToLower(); } else { foreach (var ch in revisionrpart) { alphaIndex++; if (Char.IsLetter(ch)) { hasAlpha = true; break; } } if (hasAlpha) { RevisionNumber = revisionrpart.Substring(0, alphaIndex); lastPart = revisionrpart.Substring(alphaIndex).ToLower(); } else { RevisionNumber = revisionrpart; } } } } else { var minorPart = version.Substring(firstDotindex + 1); dashIndex = minorPart.IndexOf('-'); if (dashIndex >= 0) { MinorVersion = minorPart.Substring(0, dashIndex); lastPart = minorPart.Substring(dashIndex + 1).ToLower(); } else { foreach (var ch in minorPart) { alphaIndex++; if (Char.IsLetter(ch)) { hasAlpha = true; break; } } if (hasAlpha) { MinorVersion = minorPart.Substring(0, alphaIndex); lastPart = minorPart.Substring(alphaIndex).ToLower(); } else { MinorVersion = minorPart; } } } if (!string.IsNullOrEmpty(lastPart)) { Alpha = (lastPart.Contains("alpha") || lastPart[0] == 'a'); Beta = (lastPart.Contains("beta") || lastPart[0] == 'b'); RC = (lastPart.Contains("rc")); lastPart = lastPart.Replace("alpha", "").Replace("beta", "").Replace("a", "").Replace("b", "").Replace("rc", ""); if (!string.IsNullOrEmpty(lastPart)) { Stage = SafeClrConvert.ToByte(lastPart); } } } } }
private void calculate(string duration) { hour = 0; min = 0; sec = 0; var arr = duration.Split(':'); if (arr.Length == 1) { calculate(SafeClrConvert.ToDouble(arr[0])); return; } else if (arr.Length == 2) { hour = SafeClrConvert.ToByte(arr[0]); if (min > 23) { throw new ApplicationException("invalid hour: " + arr[0]); } min = SafeClrConvert.ToByte(arr[1]); if (min > 59) { throw new ApplicationException("invalid minute: " + arr[1]); } } else if (arr.Length == 3) { hour = SafeClrConvert.ToByte(arr[0]); if (min > 23) { throw new ApplicationException("invalid hour: " + arr[0]); } min = SafeClrConvert.ToByte(arr[1]); if (min > 59) { throw new ApplicationException("invalid minute: " + arr[1]); } sec = SafeClrConvert.ToByte(arr[2]); if (sec > 59) { throw new ApplicationException("invalid second: " + arr[2]); } } else { throw new ApplicationException("invalid argument"); } this.calculatedDuration.Clear(); if (hour > 0) { this.calculatedDuration.Add(new TimeDurationValue { Value = hour, Unit = TimeDurationUnit.Hour }); } if (min > 0 || hour > 0) { this.calculatedDuration.Add(new TimeDurationValue { Value = min, Unit = TimeDurationUnit.Minute }); } if (sec > 0 || min > 0 || hour > 0) { this.calculatedDuration.Add(new TimeDurationValue { Value = sec, Unit = TimeDurationUnit.Second }); } }
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.Byte ToByte(this string s, System.Byte @default = default(System.Byte)) { return(SafeClrConvert.ToByte(s, @default)); }