public void AddCollection(object instance, object val) { object collection = Get(instance); if (collection == null) { collection = ValueType.GetConstructor(new Type[] {}).Invoke(new object[] {}); Set(instance, collection); } d_collectionAddMethod.Invoke(collection, new object[] { val }); }
internal protected override object OnCreateInstance(SerializationContext serCtx, DataNode data) { DataItem item = data as DataItem; if (item == null) { throw new InvalidOperationException("Invalid value found for type '" + Name + "'"); } DataValue ctype = item ["ctype"] as DataValue; if (ctype != null && ctype.Value != Name) { bool isFallbackType; DataType stype = FindDerivedType(ctype.Value, null, out isFallbackType); if (isFallbackType) { // Remove the ctype attribute, to make sure it is not checked again // by the fallback type item.ItemData.Remove(ctype); } if (stype != null) { object sobj = stype.CreateInstance(serCtx, data); // Store the original data type, so it can be serialized back if (isFallbackType && sobj is IExtendedDataItem) { ((IExtendedDataItem)sobj).ExtendedProperties ["__raw_ctype"] = ctype; } return(sobj); } else { throw new InvalidOperationException("Type not found: " + ctype.Value); } } ConstructorInfo ctor = ValueType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); if (ctor == null) { throw new InvalidOperationException("Default constructor not found for type '" + ValueType + "'"); } return(ctor.Invoke(null)); }
public object CreateValue() { #if !NETFX_CORE ConstructorInfo constructorInfo = ValueType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); #else var constructorInfos = ValueType.GetTypeInfo().DeclaredConstructors; ConstructorInfo constructorInfo = null; foreach (var info in constructorInfos) { if (info.GetParameters().Length == 0) { constructorInfo = info; } } #endif Debug.Assert(constructorInfo != null); return(constructorInfo.Invoke(null)); }
public object CreateValue() { ConstructorInfo constructorInfoObj = ValueType.GetConstructor(Type.EmptyTypes); return(constructorInfoObj.Invoke(null)); }
/// <inheritdoc /> public object ParseValue(string text, string stringMarker = null, IFormatProvider provider = null) { if (provider == null) { provider = CultureInfo.InvariantCulture; } if (ValueType == null) { throw new InvalidOperationException("This function requires a valid ValueType!"); } if (text == null) { return(null); } if (stringMarker != null) { if (text == "null") { return(null); } } switch (DataType) { case DataType.TimeSpan: { if (string.IsNullOrEmpty(text) || (text == "null")) { return(IsNullable ? null : (object)default(TimeSpan)); } switch (DateTimeType) { default: throw new NotSupportedException($"DateTimeType {DateTimeType} is not supported."); case DateTimeType.BigIntHumanReadable: return(new TimeSpan(DateTime.ParseExact(text, Storage.BigIntDateTimeFormat, provider).Ticks)); case DateTimeType.Undefined: case DateTimeType.Native: if (stringMarker != null) { text = text.Unbox(stringMarker, false); } #if NET20 || NET35 return(TimeSpan.Parse(text)); #else return(TimeSpan.Parse(text, provider)); #endif case DateTimeType.BigIntTicks: return(new TimeSpan(long.Parse(text, provider))); case DateTimeType.DecimalSeconds: return(new TimeSpan((long)decimal.Round(decimal.Parse(text, provider) * TimeSpan.TicksPerSecond))); case DateTimeType.DoubleSeconds: { var value = double.Parse(text, provider) * TimeSpan.TicksPerSecond; var longValue = (long)value; if ((value > 0) && (longValue < 0)) { Trace.WriteLine("DoubleSeconds exceeded (long) range. Overflow detected!"); longValue = long.MaxValue; } else if ((value < 0) && (longValue > 0)) { Trace.WriteLine("DoubleSeconds exceeded (long) range. Overflow detected!"); longValue = long.MinValue; } return(new TimeSpan(longValue)); } } } case DataType.DateTime: { if (string.IsNullOrEmpty(text) || (text == "null")) { return(IsNullable ? null : (object)default(DateTime)); } switch (DateTimeType) { default: throw new NotSupportedException($"DateTimeType {DateTimeType} is not supported."); case DateTimeType.BigIntHumanReadable: return(DateTime.ParseExact(text, Storage.BigIntDateTimeFormat, provider)); case DateTimeType.Undefined: case DateTimeType.Native: if (stringMarker != null) { text = text.Unbox(stringMarker, false); } return(DateTime.ParseExact(text, StringExtensions.InterOpDateTimeFormat, provider)); case DateTimeType.BigIntTicks: return(new DateTime(long.Parse(text, provider), DateTimeKind)); case DateTimeType.DecimalSeconds: return(new DateTime((long)decimal.Round(decimal.Parse(text, provider) * TimeSpan.TicksPerSecond), DateTimeKind)); case DateTimeType.DoubleSeconds: return(new DateTime((long)Math.Round(double.Parse(text, provider) * TimeSpan.TicksPerSecond), DateTimeKind)); case DateTimeType.DoubleEpoch: return(new DateTime((long)Math.Round(double.Parse(text, provider) * TimeSpan.TicksPerSecond) + Storage.EpochTicks, DateTimeKind)); } } case DataType.Binary: { if (string.IsNullOrEmpty(text) || (text == "null")) { return(null); } if (stringMarker != null) { text = text.Unbox(stringMarker, false); } return(Base64.NoPadding.Decode(text)); } case DataType.Bool: if (text.Length == 0) { return(IsNullable ? null : (object)false); } return((text.ToUpperInvariant() == "TRUE") || (text.ToUpperInvariant() == "YES") || (text == "1")); case DataType.Single: if (text.Length == 0) { return(IsNullable ? null : (object)0f); } return(float.Parse(text, provider)); case DataType.Double: if (text.Length == 0) { return(IsNullable ? null : (object)0d); } return(double.Parse(text, provider)); case DataType.Decimal: if (text.Length == 0) { return(IsNullable ? null : (object)0m); } return(decimal.Parse(text, provider)); case DataType.Int8: if (text.Length == 0) { return(IsNullable ? null : (object)(sbyte)0); } return(sbyte.Parse(text, provider)); case DataType.Int16: if (text.Length == 0) { return(IsNullable ? null : (object)(short)0); } return(short.Parse(text, provider)); case DataType.Int32: if (text.Length == 0) { return(IsNullable ? null : (object)0); } return(int.Parse(text, provider)); case DataType.Int64: if (text.Length == 0) { return(IsNullable ? null : (object)0L); } return(long.Parse(text, provider)); case DataType.UInt8: if (text.Length == 0) { return(IsNullable ? null : (object)(byte)0); } return(byte.Parse(text, provider)); case DataType.UInt16: if (text.Length == 0) { return(IsNullable ? null : (object)(ushort)0); } return(ushort.Parse(text, provider)); case DataType.UInt32: if (text.Length == 0) { return(IsNullable ? null : (object)0U); } return(uint.Parse(text, provider)); case DataType.UInt64: if (text.Length == 0) { return(IsNullable ? null : (object)0UL); } return(ulong.Parse(text, provider)); case DataType.Enum: if (stringMarker != null) { text = text.Unbox(stringMarker, false); } if (text.Length == 0) { text = "0"; } return(Enum.Parse(ValueType, text, true)); case DataType.Char: if (stringMarker != null) { text = text.Unbox(stringMarker, false).Unescape(); } if (text.Length != 1) { throw new InvalidDataException(); } return(text[0]); case DataType.String: if (stringMarker != null) { text = text.Unbox(stringMarker, false).Unescape(); } return(text); case DataType.User: break; default: throw new NotImplementedException(); } if (stringMarker != null) { text = text.Unbox(stringMarker, false).Unescape(); } if (!parserInitialized) { // lookup static Parse(string) method first staticParse = ValueType.GetMethod("Parse", BindingFlags.Public | BindingFlags.Static, null, new[] { typeof(string) }, null); // if there is none, search constructor(string) if (staticParse == null) { constructor = ValueType.GetConstructor(new[] { typeof(string) }); } parserInitialized = true; } // has static Parse(string) ? if (staticParse != null) { // use method to parse value return(staticParse.Invoke(null, new object[] { text })); } // has constructor(string) ? if (constructor != null) { return(constructor.Invoke(new object[] { text })); } throw new MissingMethodException($"Could not find a way to parse or create {ValueType} from string!"); }