public EditableInfo(PropertyInfo f) { EditablePreferenceAttribute attr = (EditablePreferenceAttribute)f.GetCustomAttributes(typeof(EditablePreferenceAttribute), false).First(); Accessor = new StringAccessor(f); DirectAccessor = Accessors.CreateAccessor(f); Name = (attr.Name ?? f.Name).ToLower(); CanUnset = attr.CanUnset; Type = f.PropertyType; Default = Type.IsValueType ? Activator.CreateInstance(Type) : null; }
/// <summary> /// Determines whether this instance and a specified object have the same value. /// </summary> /// <param name="obj">The StringAccessor to compare to this instance.</param> /// <returns>true if obj is a StringAccessor and its value is the same as this instance; otherwise, false.</returns> public override bool Equals(object obj) { if (obj == null) { return(false); } StringAccessor b = obj as StringAccessor; if ((object)b == null) { string s = obj as string; if ((object)s == null) { return(false); } return(this.ToString() == s); } return(this.ToString() == b.ToString()); }
/// <summary> /// Creates an accessor depending on the format. /// </summary> /// <param name="start">the zero-based index of the first byte of the accessor</param> /// <param name="length">the length of the accessor</param> /// <param name="format">the format of the accessor</param> /// <param name="encoding">the encoding of the accessed records</param> /// <returns>a new accessor with the correct parameters</returns> protected object GetAccessor(int start, int length, string format, Encoding encoding) { object result; switch (format) { case StringFormat: case SubstringFormat: result = new StringAccessor { Encoding = encoding, Start = start, Length = length }; break; case ZonedFormat: result = new ZonedAccessor { Encoding = encoding, Length = length, Start = start }; break; case PackedFormat: result = new PackedAccessor { Encoding = encoding, Length = length, Start = start }; break; case SignedBinaryFormat: result = new BinaryAccessor { Encoding = encoding, Length = length, Start = start, Signed = true }; break; case BinaryFormat: result = new BinaryAccessor { Encoding = encoding, Length = length, Start = start, Signed = false }; break; default: throw new ParsingException("Unknown format: " + format); } return(result); }
/// <summary> /// Creates an accessor depending on the format. /// </summary> /// <param name="start">the zero-based index of the first byte of the accessor</param> /// <param name="length">the length of the accessor</param> /// <param name="format">the format of the accessor</param> /// <param name="encoding">the encoding of the accessed records</param> /// <returns>a new accessor with the correct parameters</returns> protected object GetAccessor(int start, int length, string format, Encoding encoding) { object result; switch (format) { case StringFormat: case SubstringFormat: result = new StringAccessor { Encoding = encoding, Start = start, Length = length }; break; case ZonedFormat: result = new ZonedAccessor { Encoding = encoding, Length = length, Start = start }; break; case PackedFormat: result = new PackedAccessor { Encoding = encoding, Length = length, Start = start }; break; case SignedBinaryFormat: result = new BinaryAccessor { Encoding = encoding, Length = length, Start = start, Signed = true }; break; case BinaryFormat: result = new BinaryAccessor { Encoding = encoding, Length = length, Start = start, Signed = false }; break; default: throw new ParsingException("Unknown format: " + format); } return result; }
private void InitInfo(Type modelType) { accessorDict = new Dictionary <string, Accessor>(); var Properties = TypeCache.GetTable(modelType).Fields; foreach (var kv in Properties) { Accessor accessor = null; var prop = kv.GetPropertyInfo(); string propName = prop.Name.ToUpper(); var propType = prop.PropertyType; if (propType.IsEnum) { propType = propType.GetEnumUnderlyingType(); } if (typeof(string) == propType) { accessor = new StringAccessor(prop); } else if (typeof(int) == propType) { accessor = new IntAccessor(prop); } else if (typeof(int?) == propType) { accessor = new IntNullableAccessor(prop); } else if (typeof(DateTime) == propType) { accessor = new DateTimeAccessor(prop); } else if (typeof(DateTime?) == propType) { accessor = new DateTimeNullableAccessor(prop); } else if (typeof(long) == propType) { accessor = new LongAccessor(prop); } else if (typeof(long?) == propType) { accessor = new LongNullableAccessor(prop); } else if (typeof(float) == propType) { accessor = new FloatAccessor(prop); } else if (typeof(float?) == propType) { accessor = new FloatNullableAccessor(prop); } else if (typeof(double) == propType) { accessor = new DoubleAccessor(prop); } else if (typeof(double?) == propType) { accessor = new DoubleNullableAccessor(prop); } else if (typeof(Guid) == propType) { accessor = new GuidAccessor(prop); } else if (typeof(Guid?) == propType) { accessor = new GuidNullableAccessor(prop); } else if (typeof(short) == propType) { accessor = new ShortAccessor(prop); } else if (typeof(short?) == propType) { accessor = new ShortNullableAccessor(prop); } else if (typeof(byte) == propType) { accessor = new ByteAccessor(prop); } else if (typeof(byte?) == propType) { accessor = new ByteNullableAccessor(prop); } else if (typeof(char) == propType) { accessor = new CharAccessor(prop); } else if (typeof(char?) == propType) { accessor = new CharNullableAccessor(prop); } else if (typeof(decimal) == propType) { accessor = new DecimalAccessor(prop); } else if (typeof(decimal?) == propType) { accessor = new DecimalNullableAccessor(prop); } else if (typeof(byte[]) == propType) { accessor = new ByteArrayAccessor(prop); } else if (typeof(bool) == propType) { accessor = new BoolAccessor(prop); } else if (typeof(bool?) == propType) { accessor = new BoolNullableAccessor(prop); } else if (typeof(TimeSpan) == propType) { accessor = new TimeSpanAccessor(prop); } else if (typeof(TimeSpan?) == propType) { accessor = new TimeSpanNullableAccessor(prop); } accessorDict[propName] = accessor; } }
private void InitInfo() { accessorDict = new Dictionary <string, Accessor>(Properties.Length); FieldAttrDict = new Dictionary <PropertyInfo, OrmFieldAttribute>(Properties.Length); foreach (var prop in Properties) { Accessor accessor = null; string propName = prop.Name.ToUpper(); var propType = prop.PropertyType; if (propType.IsEnum) { propType = propType.GetEnumUnderlyingType(); } if (typeof(string) == propType) { accessor = new StringAccessor(prop); } else if (typeof(int) == propType) { accessor = new IntAccessor(prop); } else if (typeof(int?) == propType) { accessor = new IntNullableAccessor(prop); } else if (typeof(DateTime) == propType) { accessor = new DateTimeAccessor(prop); } else if (typeof(DateTime?) == propType) { accessor = new DateTimeNullableAccessor(prop); } else if (typeof(long) == propType) { accessor = new LongAccessor(prop); } else if (typeof(long?) == propType) { accessor = new LongNullableAccessor(prop); } else if (typeof(float) == propType) { accessor = new FloatAccessor(prop); } else if (typeof(float?) == propType) { accessor = new FloatNullableAccessor(prop); } else if (typeof(double) == propType) { accessor = new DoubleAccessor(prop); } else if (typeof(double?) == propType) { accessor = new DoubleNullableAccessor(prop); } else if (typeof(Guid) == propType) { accessor = new GuidAccessor(prop); } else if (typeof(Guid?) == propType) { accessor = new GuidNullableAccessor(prop); } else if (typeof(short) == propType) { accessor = new ShortAccessor(prop); } else if (typeof(short?) == propType) { accessor = new ShortNullableAccessor(prop); } else if (typeof(byte) == propType) { accessor = new ByteAccessor(prop); } else if (typeof(byte?) == propType) { accessor = new ByteNullableAccessor(prop); } else if (typeof(char) == propType) { accessor = new CharAccessor(prop); } else if (typeof(char?) == propType) { accessor = new CharNullableAccessor(prop); } else if (typeof(decimal) == propType) { accessor = new DecimalAccessor(prop); } else if (typeof(decimal?) == propType) { accessor = new DecimalNullableAccessor(prop); } else if (typeof(byte[]) == propType) { accessor = new ByteArrayAccessor(prop); } else if (typeof(bool) == propType) { accessor = new BoolAccessor(prop); } else if (typeof(bool?) == propType) { accessor = new BoolNullableAccessor(prop); } else if (typeof(TimeSpan) == propType) { accessor = new TimeSpanAccessor(prop); } else if (typeof(TimeSpan?) == propType) { accessor = new TimeSpanNullableAccessor(prop); } accessorDict[propName] = accessor; //自定义属性 var customerAttributes = prop.GetCustomAttributes(typeof(OrmFieldAttribute), false); if (customerAttributes == null || customerAttributes.Length == 0) { FieldAttrDict[prop] = null; } else { FieldAttrDict[prop] = (OrmFieldAttribute)customerAttributes[0]; } } }
/// <summary> /// Creates an accessor depending on the format. /// </summary> /// <param name="start">the zero-based index of the first byte of the accessor</param> /// <param name="length">the length of the accessor</param> /// <param name="format">the format of the accessor</param> /// <param name="encoding">the encoding of the accessed records</param> /// <returns>a new accessor with the correct parameters</returns> protected object GetAccessor(int start, int length, string format, Encoding encoding) { object result; switch (format) { case StringFormat: case SubstringFormat: result = new StringAccessor { Encoding = encoding, Start = start, Length = length }; break; case ZonedFormat: result = new ZonedAccessor { Encoding = encoding, Length = length, Start = start }; break; case PackedFormat: result = new PackedAccessor { Encoding = encoding, Length = length, Start = start }; break; case SignedBinaryFormat: result = new BinaryAccessor { Encoding = encoding, Length = length, Start = start, Signed = true }; break; case BinaryFormat: result = new BinaryAccessor { Encoding = encoding, Length = length, Start = start, Signed = false }; break; default: if (format != null && format.StartsWith(AsciiDecimalFormat)) { int precision = 0; if (format.Length > 2) { precision = int.Parse(format.Substring(2)); result = new AsciiAcessor { Encoding = encoding, Length = length, Start = start, Precision = precision }; } else { throw new ParsingException("Unknown format: " + format); } break; } else { throw new ParsingException("Unknown format: " + format); } } return(result); }