private static Union CreateUnion(Type t) { Union union = new Union(); object[] args = new object[2]; args[0] = t; union.TagReader = (FSharpFunction)FSharpUtils.PreComputeUnionTagReader(null, args); union.Cases = new List <UnionCase>(); object[] objArray2 = new object[2]; objArray2[0] = t; foreach (object obj2 in (object[])FSharpUtils.GetUnionCases(null, objArray2)) { UnionCase item = new UnionCase { Tag = (int)FSharpUtils.GetUnionCaseInfoTag(obj2), Name = (string)FSharpUtils.GetUnionCaseInfoName(obj2), Fields = (PropertyInfo[])FSharpUtils.GetUnionCaseInfoFields(obj2, new object[0]) }; object[] objArray3 = new object[2]; objArray3[0] = obj2; item.FieldReader = (FSharpFunction)FSharpUtils.PreComputeUnionReader(null, objArray3); object[] objArray4 = new object[2]; objArray4[0] = obj2; item.Constructor = (FSharpFunction)FSharpUtils.PreComputeUnionConstructor(null, objArray4); union.Cases.Add(item); } return(union); }
public override bool CanConvert(Type objectType) { if (typeof(IEnumerable).IsAssignableFrom(objectType)) { return(false); } bool flag = false; object[] customAttributes = objectType.GetCustomAttributes(true); for (int i = 0; i < customAttributes.Length; i++) { Type type = customAttributes[i].GetType(); if (type.FullName == "Microsoft.FSharp.Core.CompilationMappingAttribute") { FSharpUtils.EnsureInitialized(type.Assembly()); flag = true; break; } } if (!flag) { return(false); } object[] args = new object[2]; args[0] = objectType; return((bool)FSharpUtils.IsUnion(null, args)); }
/// <summary> /// Determines whether this instance can convert the specified object type. /// </summary> /// <param name="objectType">Type of the object.</param> /// <returns> /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>. /// </returns> public override bool CanConvert(Type objectType) { if (typeof(IEnumerable).IsAssignableFrom(objectType)) { return(false); } // all fsharp objects have CompilationMappingAttribute // get the fsharp assembly from the attribute and initialize latebound methods var attributes = objectType.GetCustomAttributes(true); var isFSharpType = false; foreach (var attribute in attributes) { var attributeType = attribute.GetType(); if (attributeType.FullName == "Microsoft.FSharp.Core.CompilationMappingAttribute") { FSharpUtils.EnsureInitialized(attributeType.Assembly()); isFSharpType = true; break; } } if (!isFSharpType) { return(false); } return((bool)FSharpUtils.IsUnion(null, objectType, null)); }
private static Union CreateUnion(Type t) { Union u = new Union { TagReader = (FSharpFunction)FSharpUtils.PreComputeUnionTagReader(null, t, null), Cases = new List <UnionCase>() }; object[] cases = (object[])FSharpUtils.GetUnionCases(null, t, null); foreach (object unionCaseInfo in cases) { UnionCase unionCase = new UnionCase { Tag = (int)FSharpUtils.GetUnionCaseInfoTag(unionCaseInfo), Name = (string)FSharpUtils.GetUnionCaseInfoName(unionCaseInfo), Fields = (PropertyInfo[])FSharpUtils.GetUnionCaseInfoFields(unionCaseInfo), FieldReader = (FSharpFunction)FSharpUtils.PreComputeUnionReader(null, unionCaseInfo, null), Constructor = (FSharpFunction)FSharpUtils.PreComputeUnionConstructor(null, unionCaseInfo, null) }; u.Cases.Add(unionCase); } return(u); }
/// <summary> /// Determines whether this instance can convert the specified object type. /// </summary> /// <param name="objectType">Type of the object.</param> /// <returns> /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>. /// </returns> public override bool CanConvert(Type objectType) { if (typeof(IEnumerable).IsAssignableFrom(objectType)) { return(false); } // all fsharp objects have CompilationMappingAttribute // get the fsharp assembly from the attribute and initialize latebound methods object[] attributes; #if !(NETFX_CORE || PORTABLE) attributes = objectType.GetCustomAttributes(true); #else attributes = objectType.GetTypeInfo().GetCustomAttributes(true).ToArray(); #endif bool isFSharpType = false; foreach (object attribute in attributes) { Type attributeType = attribute.GetType(); if (attributeType.Name == "CompilationMappingAttribute") { FSharpUtils.EnsureInitialized(attributeType.Assembly()); isFSharpType = true; break; } } if (!isFSharpType) { return(false); } return((bool)FSharpUtils.IsUnion(null, objectType, null)); }
// Token: 0x060014A2 RID: 5282 RVA: 0x0006DAB0 File Offset: 0x0006BCB0 public override bool CanConvert(Type objectType) { if (typeof(IEnumerable).IsAssignableFrom(objectType)) { return(false); } object[] customAttributes = objectType.GetCustomAttributes(true); bool flag = false; object[] array = customAttributes; for (int i = 0; i < array.Length; i++) { Type type = array[i].GetType(); if (type.FullName == "Microsoft.FSharp.Core.CompilationMappingAttribute") { FSharpUtils.EnsureInitialized(type.Assembly()); flag = true; break; } } if (!flag) { return(false); } MethodCall <object, object> isUnion = FSharpUtils.Instance.IsUnion; object target = null; object[] array2 = new object[2]; array2[0] = objectType; return((bool)isUnion(target, array2)); }
private void StoreFSharpListCreatorIfNecessary(Type underlyingType) { if (!this.HasParameterizedCreatorInternal && underlyingType.Name == "FSharpList`1") { FSharpUtils.EnsureInitialized(underlyingType.Assembly()); this._parameterizedCreator = FSharpUtils.Instance.CreateSeq(this.CollectionItemType); } }
private void StoreFSharpListCreatorIfNecessary(Type underlyingType) { if (!HasParameterizedCreatorInternal && underlyingType.Name == FSharpUtils.FSharpListTypeName) { FSharpUtils.EnsureInitialized(underlyingType.Assembly()); _parameterizedCreator = FSharpUtils.CreateSeq(CollectionItemType); } }
private static Type CreateUnionTypeLookup(Type t) { object[] args = new object[2]; args[0] = t; object arg = ((object[])FSharpUtils.GetUnionCases(null, args)).First <object>(); return((Type)FSharpUtils.GetUnionCaseInfoDeclaringType(arg)); }
public JsonDictionaryContract(Type underlyingType) : base(underlyingType) { Type type; Type type2; base.ContractType = JsonContractType.Dictionary; if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary <,>), out this._genericCollectionDefinitionType)) { type = this._genericCollectionDefinitionType.GetGenericArguments()[0]; type2 = this._genericCollectionDefinitionType.GetGenericArguments()[1]; if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IDictionary <,>))) { Type[] typeArguments = new Type[] { type, type2 }; base.CreatedType = typeof(Dictionary <,>).MakeGenericType(typeArguments); } base.IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyDictionary <,>)); } else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyDictionary <,>), out this._genericCollectionDefinitionType)) { type = this._genericCollectionDefinitionType.GetGenericArguments()[0]; type2 = this._genericCollectionDefinitionType.GetGenericArguments()[1]; if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IReadOnlyDictionary <,>))) { Type[] typeArguments = new Type[] { type, type2 }; base.CreatedType = typeof(ReadOnlyDictionary <,>).MakeGenericType(typeArguments); } base.IsReadOnlyOrFixedSize = true; } else { ReflectionUtils.GetDictionaryKeyValueTypes(base.UnderlyingType, out type, out type2); if (base.UnderlyingType == typeof(IDictionary)) { base.CreatedType = typeof(Dictionary <object, object>); } } if ((type != null) && (type2 != null)) { Type[] typeArguments = new Type[] { type, type2 }; Type[] typeArray4 = new Type[] { type, type2 }; this._parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(base.CreatedType, typeof(KeyValuePair <,>).MakeGenericType(typeArguments), typeof(IDictionary <,>).MakeGenericType(typeArray4)); if (!this.HasParameterizedCreatorInternal && (underlyingType.Name == "FSharpMap`2")) { FSharpUtils.EnsureInitialized(underlyingType.Assembly()); this._parameterizedCreator = FSharpUtils.CreateMap(type, type2); } } this.ShouldCreateWrapper = !typeof(IDictionary).IsAssignableFrom(base.CreatedType); this.DictionaryKeyType = type; this.DictionaryValueType = type2; if (ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(underlyingType, this.DictionaryKeyType, this.DictionaryValueType, out Type type3, out ObjectConstructor <object> constructor)) { base.CreatedType = type3; this._parameterizedCreator = constructor; base.IsReadOnlyOrFixedSize = true; } }
private static Type CreateUnionTypeLookup(Type t) { // this lookup is because cases with fields are derived from union type // need to get declaring type to avoid duplicate Unions in cache // hacky but I can't find an API to get the declaring type without GetUnionCases object[] cases = (object[])FSharpUtils.GetUnionCases(null, t, null); object caseInfo = cases.First(); Type unionType = (Type)FSharpUtils.GetUnionCaseInfoDeclaringType(caseInfo); return(unionType); }
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing value of object being read.</param> /// <param name="serializer">The calling serializer.</param> /// <returns>The object value.</returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } IEnumerable cases = (IEnumerable)FSharpUtils.GetUnionCases(null, objectType, null); ReadAndAssertProperty(reader, CasePropertyName); ReadAndAssert(reader); string caseName = reader.Value.ToString(); object matchingCaseInfo = null; foreach (object c in cases) { if ((string)FSharpUtils.GetUnionCaseInfoName(c) == caseName) { matchingCaseInfo = c; break; } } if (matchingCaseInfo == null) { throw new JsonSerializationException("No union type found with the name '{0}'.".FormatWith(CultureInfo.InvariantCulture, caseName)); } ReadAndAssertProperty(reader, FieldsPropertyName); // start array ReadAndAssert(reader); // first value ReadAndAssert(reader); PropertyInfo[] fieldProperties = (PropertyInfo[])FSharpUtils.GetUnionCaseInfoFields(matchingCaseInfo); List <object> fieldValues = new List <object>(); foreach (PropertyInfo field in fieldProperties) { fieldValues.Add(serializer.Deserialize(reader, field.PropertyType)); ReadAndAssert(reader); } // end object ReadAndAssert(reader); return(FSharpUtils.MakeUnion(null, matchingCaseInfo, fieldValues.ToArray(), null)); }
/// <summary> /// Writes the JSON representation of the object. /// </summary> /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> /// <param name="value">The value.</param> /// <param name="serializer">The calling serializer.</param> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver; Type t = value.GetType(); object result = FSharpUtils.GetUnionFields(null, value, t, null); object info = FSharpUtils.GetUnionCaseInfo(result); object fields = FSharpUtils.GetUnionCaseFields(result); object caseName = FSharpUtils.GetUnionCaseInfoName(info); writer.WriteStartObject(); writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(CasePropertyName) : CasePropertyName); writer.WriteValue((string)caseName); writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(FieldsPropertyName) : FieldsPropertyName); serializer.Serialize(writer, fields); writer.WriteEndObject(); }
/// <summary> /// Initializes a new instance of the <see cref="JsonDictionaryContract"/> class. /// </summary> /// <param name="underlyingType">The underlying type for the contract.</param> public JsonDictionaryContract(Type underlyingType) : base(underlyingType) { ContractType = JsonContractType.Dictionary; Type keyType; Type valueType; if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary <,>), out _genericCollectionDefinitionType)) { keyType = _genericCollectionDefinitionType.GetGenericArguments()[0]; valueType = _genericCollectionDefinitionType.GetGenericArguments()[1]; if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IDictionary <,>))) { CreatedType = typeof(Dictionary <,>).MakeGenericType(keyType, valueType); } } else { ReflectionUtils.GetDictionaryKeyValueTypes(UnderlyingType, out keyType, out valueType); if (UnderlyingType == typeof(IDictionary)) { CreatedType = typeof(Dictionary <object, object>); } } if (keyType != null && valueType != null) { _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, typeof(KeyValuePair <,>).MakeGenericType(keyType, valueType)); if (!HasParametrizedCreator && underlyingType.Name == FSharpUtils.FSharpMapTypeName) { FSharpUtils.EnsureInitialized(underlyingType.Assembly()); _parametrizedCreator = FSharpUtils.CreateMap(keyType, valueType); } } ShouldCreateWrapper = !typeof(IDictionary).IsAssignableFrom(CreatedType); DictionaryKeyType = keyType; DictionaryValueType = valueType; }
public override bool CanConvert(Type objectType) { if (typeof(IEnumerable).IsAssignableFrom(objectType)) { return(false); } bool flag = false; object[] customAttributes = objectType.GetCustomAttributes(true); int num = 0; while (true) { if (num < (int)customAttributes.Length) { Type type = customAttributes[num].GetType(); if (type.FullName == "Microsoft.FSharp.Core.CompilationMappingAttribute") { FSharpUtils.EnsureInitialized(type.Assembly()); flag = true; break; } else { num++; } } else { break; } } if (!flag) { return(false); } return((bool)FSharpUtils.IsUnion(null, new object[] { objectType, null })); }
private static DiscriminatedUnionConverter.Union CreateUnion(Type t) { DiscriminatedUnionConverter.Union union = new DiscriminatedUnionConverter.Union() { TagReader = (FSharpFunction)FSharpUtils.PreComputeUnionTagReader(null, new object[] { t, null }), Cases = new List <DiscriminatedUnionConverter.UnionCase>() }; object[] getUnionCases = (object[])FSharpUtils.GetUnionCases(null, new object[] { t, null }); for (int i = 0; i < (int)getUnionCases.Length; i++) { object obj = getUnionCases[i]; DiscriminatedUnionConverter.UnionCase unionCase = new DiscriminatedUnionConverter.UnionCase() { Tag = (int)FSharpUtils.GetUnionCaseInfoTag(obj), Name = (string)FSharpUtils.GetUnionCaseInfoName(obj), Fields = (PropertyInfo[])FSharpUtils.GetUnionCaseInfoFields(obj, new object[0]), FieldReader = (FSharpFunction)FSharpUtils.PreComputeUnionReader(null, new object[] { obj, null }), Constructor = (FSharpFunction)FSharpUtils.PreComputeUnionConstructor(null, new object[] { obj, null }) }; union.Cases.Add(unionCase); } return(union); }
private static Union CreateUnion(Type t) { Union u = new Union(); u.TagReader = (Converter <object, int>)FSharpUtils.PreComputeUnionTagReader(null, t, null); u.Cases = new List <UnionCase>(); object[] cases = (object[])FSharpUtils.GetUnionCases(null, t, null); foreach (object unionCaseInfo in cases) { UnionCase unionCase = new UnionCase(); unionCase.Tag = (int)FSharpUtils.GetUnionCaseInfoTag(unionCaseInfo); unionCase.Name = (string)FSharpUtils.GetUnionCaseInfoName(unionCaseInfo); unionCase.Fields = (PropertyInfo[])FSharpUtils.GetUnionCaseInfoFields(unionCaseInfo); unionCase.FieldReader = (Converter <object, object[]>)FSharpUtils.PreComputeUnionReader(null, unionCaseInfo, null); unionCase.Constructor = (Converter <object[], object>)FSharpUtils.PreComputeUnionConstructor(null, unionCaseInfo, null); u.Cases.Add(unionCase); } return(u); }
/// <summary> /// Initializes a new instance of the <see cref="JsonDictionaryContract"/> class. /// </summary> /// <param name="underlyingType">The underlying type for the contract.</param> public JsonDictionaryContract(Type underlyingType) : base(underlyingType) { ContractType = JsonContractType.Dictionary; Type keyType; Type valueType; if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary <,>), out _genericCollectionDefinitionType)) { keyType = _genericCollectionDefinitionType.GetGenericArguments()[0]; valueType = _genericCollectionDefinitionType.GetGenericArguments()[1]; if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IDictionary <,>))) { CreatedType = typeof(Dictionary <,>).MakeGenericType(keyType, valueType); } #if !(NET40 || NET35 || NET20 || PORTABLE40) IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyDictionary <,>)); #endif } #if !(NET40 || NET35 || NET20 || PORTABLE40) else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyDictionary <,>), out _genericCollectionDefinitionType)) { keyType = _genericCollectionDefinitionType.GetGenericArguments()[0]; valueType = _genericCollectionDefinitionType.GetGenericArguments()[1]; if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IReadOnlyDictionary <,>))) { CreatedType = typeof(ReadOnlyDictionary <,>).MakeGenericType(keyType, valueType); } IsReadOnlyOrFixedSize = true; } #endif else { ReflectionUtils.GetDictionaryKeyValueTypes(UnderlyingType, out keyType, out valueType); if (UnderlyingType == typeof(IDictionary)) { CreatedType = typeof(Dictionary <object, object>); } } if (keyType != null && valueType != null) { _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, typeof(KeyValuePair <,>).MakeGenericType(keyType, valueType)); #if !(NET35 || NET20) if (!HasParametrizedCreator && underlyingType.Name == FSharpUtils.FSharpMapTypeName) { FSharpUtils.EnsureInitialized(underlyingType.Assembly()); _parametrizedCreator = FSharpUtils.CreateMap(keyType, valueType); } #endif } ShouldCreateWrapper = !typeof(IDictionary).IsAssignableFrom(CreatedType); DictionaryKeyType = keyType; DictionaryValueType = valueType; #if (NET20 || NET35) if (DictionaryValueType != null && ReflectionUtils.IsNullableType(DictionaryValueType)) { Type tempDictioanryType; // bug in .NET 2.0 & 3.5 that Dictionary<TKey, Nullable<TValue>> throws an error when adding null via IDictionary[key] = object // wrapper will handle calling Add(T) instead if (ReflectionUtils.InheritsGenericDefinition(CreatedType, typeof(Dictionary <,>), out tempDictioanryType)) { ShouldCreateWrapper = true; } } #endif #if !(NET20 || NET35 || NET40) Type immutableCreatedType; ObjectConstructor <object> immutableParameterizedCreator; if (ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(underlyingType, DictionaryKeyType, DictionaryValueType, out immutableCreatedType, out immutableParameterizedCreator)) { CreatedType = immutableCreatedType; _parametrizedCreator = immutableParameterizedCreator; IsReadOnlyOrFixedSize = true; } #endif }
/// <summary> /// Initializes a new instance of the <see cref="JsonArrayContract"/> class. /// </summary> /// <param name="underlyingType">The underlying type for the contract.</param> public JsonArrayContract(Type underlyingType) : base(underlyingType) { ContractType = JsonContractType.Array; IsArray = CreatedType.IsArray; bool canDeserialize; Type tempCollectionType; if (IsArray) { CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType); IsReadOnlyOrFixedSize = true; _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType); canDeserialize = true; IsMultidimensionalArray = (IsArray && UnderlyingType.GetArrayRank() > 1); } else if (typeof(IList).IsAssignableFrom(underlyingType)) { if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType)) { CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0]; } else { CollectionItemType = ReflectionUtils.GetCollectionItemType(underlyingType); } if (underlyingType == typeof(IList)) { CreatedType = typeof(List <object>); } if (CollectionItemType != null) { _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType); } IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection <>)); canDeserialize = true; } else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType)) { CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0]; if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ICollection <>)) || ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IList <>))) { CreatedType = typeof(List <>).MakeGenericType(CollectionItemType); } #if !(NET20 || NET35) if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ISet <>))) { CreatedType = typeof(HashSet <>).MakeGenericType(CollectionItemType); } #endif _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType); canDeserialize = true; ShouldCreateWrapper = true; } #if !(NET40 || NET35 || NET20 || PORTABLE40) else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyCollection <>), out tempCollectionType)) { CollectionItemType = tempCollectionType.GetGenericArguments()[0]; if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyCollection <>)) || ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyList <>))) { CreatedType = typeof(ReadOnlyCollection <>).MakeGenericType(CollectionItemType); } _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType); _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(CreatedType, CollectionItemType); IsReadOnlyOrFixedSize = true; canDeserialize = HasParameterizedCreatorInternal; } #endif else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IEnumerable <>), out tempCollectionType)) { CollectionItemType = tempCollectionType.GetGenericArguments()[0]; if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable <>))) { CreatedType = typeof(List <>).MakeGenericType(CollectionItemType); } _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType); #if !(NET35 || NET20) if (!HasParameterizedCreatorInternal && underlyingType.Name == FSharpUtils.FSharpListTypeName) { FSharpUtils.EnsureInitialized(underlyingType.Assembly()); _parameterizedCreator = FSharpUtils.CreateSeq(CollectionItemType); } #endif if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { _genericCollectionDefinitionType = tempCollectionType; IsReadOnlyOrFixedSize = false; ShouldCreateWrapper = false; canDeserialize = true; } else { _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType); IsReadOnlyOrFixedSize = true; ShouldCreateWrapper = true; canDeserialize = HasParameterizedCreatorInternal; } } else { // types that implement IEnumerable and nothing else canDeserialize = false; ShouldCreateWrapper = true; } CanDeserialize = canDeserialize; #if (NET20 || NET35) if (CollectionItemType != null && ReflectionUtils.IsNullableType(CollectionItemType)) { // bug in .NET 2.0 & 3.5 that List<Nullable<T>> throws an error when adding null via IList.Add(object) // wrapper will handle calling Add(T) instead if (ReflectionUtils.InheritsGenericDefinition(CreatedType, typeof(List <>), out tempCollectionType) || (IsArray && !IsMultidimensionalArray)) { ShouldCreateWrapper = true; } } #endif #if !(NET20 || NET35 || NET40) Type immutableCreatedType; ObjectConstructor <object> immutableParameterizedCreator; if (ImmutableCollectionsUtils.TryBuildImmutableForArrayContract(underlyingType, CollectionItemType, out immutableCreatedType, out immutableParameterizedCreator)) { CreatedType = immutableCreatedType; _parameterizedCreator = immutableParameterizedCreator; IsReadOnlyOrFixedSize = true; CanDeserialize = true; } #endif }
public JsonArrayContract(Type underlyingType) : base(underlyingType) { bool hasParameterizedCreatorInternal; base.ContractType = JsonContractType.Array; this.IsArray = base.CreatedType.IsArray; if (this.IsArray) { this.CollectionItemType = ReflectionUtils.GetCollectionItemType(base.UnderlyingType); base.IsReadOnlyOrFixedSize = true; Type[] typeArguments = new Type[] { this.CollectionItemType }; this._genericCollectionDefinitionType = typeof(List <>).MakeGenericType(typeArguments); hasParameterizedCreatorInternal = true; this.IsMultidimensionalArray = this.IsArray && (base.UnderlyingType.GetArrayRank() > 1); } else if (typeof(IList).IsAssignableFrom(underlyingType)) { if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out this._genericCollectionDefinitionType)) { this.CollectionItemType = this._genericCollectionDefinitionType.GetGenericArguments()[0]; } else { this.CollectionItemType = ReflectionUtils.GetCollectionItemType(underlyingType); } if (underlyingType == typeof(IList)) { base.CreatedType = typeof(List <object>); } if (this.CollectionItemType != null) { this._parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, this.CollectionItemType); } base.IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection <>)); hasParameterizedCreatorInternal = true; } else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out this._genericCollectionDefinitionType)) { this.CollectionItemType = this._genericCollectionDefinitionType.GetGenericArguments()[0]; if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ICollection <>)) || ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IList <>))) { Type[] typeArguments = new Type[] { this.CollectionItemType }; base.CreatedType = typeof(List <>).MakeGenericType(typeArguments); } if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ISet <>))) { Type[] typeArguments = new Type[] { this.CollectionItemType }; base.CreatedType = typeof(HashSet <>).MakeGenericType(typeArguments); } this._parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, this.CollectionItemType); hasParameterizedCreatorInternal = true; this.ShouldCreateWrapper = true; } else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyCollection <>), out Type type)) { this.CollectionItemType = type.GetGenericArguments()[0]; if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyCollection <>)) || ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IReadOnlyList <>))) { Type[] typeArray4 = new Type[] { this.CollectionItemType }; base.CreatedType = typeof(ReadOnlyCollection <>).MakeGenericType(typeArray4); } Type[] typeArguments = new Type[] { this.CollectionItemType }; this._genericCollectionDefinitionType = typeof(List <>).MakeGenericType(typeArguments); this._parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(base.CreatedType, this.CollectionItemType); base.IsReadOnlyOrFixedSize = true; hasParameterizedCreatorInternal = this.HasParameterizedCreatorInternal; } else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IEnumerable <>), out type)) { this.CollectionItemType = type.GetGenericArguments()[0]; if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IEnumerable <>))) { Type[] typeArguments = new Type[] { this.CollectionItemType }; base.CreatedType = typeof(List <>).MakeGenericType(typeArguments); } this._parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, this.CollectionItemType); if (!this.HasParameterizedCreatorInternal && (underlyingType.Name == "FSharpList`1")) { FSharpUtils.EnsureInitialized(underlyingType.Assembly()); this._parameterizedCreator = FSharpUtils.CreateSeq(this.CollectionItemType); } if (underlyingType.IsGenericType() && (underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable <>))) { this._genericCollectionDefinitionType = type; base.IsReadOnlyOrFixedSize = false; this.ShouldCreateWrapper = false; hasParameterizedCreatorInternal = true; } else { Type[] typeArguments = new Type[] { this.CollectionItemType }; this._genericCollectionDefinitionType = typeof(List <>).MakeGenericType(typeArguments); base.IsReadOnlyOrFixedSize = true; this.ShouldCreateWrapper = true; hasParameterizedCreatorInternal = this.HasParameterizedCreatorInternal; } } else { hasParameterizedCreatorInternal = false; this.ShouldCreateWrapper = true; } this.CanDeserialize = hasParameterizedCreatorInternal; if (ImmutableCollectionsUtils.TryBuildImmutableForArrayContract(underlyingType, this.CollectionItemType, out Type type2, out ObjectConstructor <object> constructor)) { base.CreatedType = type2; this._parameterizedCreator = constructor; base.IsReadOnlyOrFixedSize = true; this.CanDeserialize = true; } }
private static Type CreateUnionTypeLookup(Type t) { object obj = ((object[])FSharpUtils.GetUnionCases(null, new object[] { t, null })).First <object>(); return((Type)FSharpUtils.GetUnionCaseInfoDeclaringType(obj)); }
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing value of object being read.</param> /// <param name="serializer">The calling serializer.</param> /// <returns>The object value.</returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } object matchingCaseInfo = null; string caseName = null; JArray fields = null; // start object ReadAndAssert(reader); while (reader.TokenType == JsonToken.PropertyName) { string propertyName = reader.Value.ToString(); if (string.Equals(propertyName, CasePropertyName, StringComparison.OrdinalIgnoreCase)) { ReadAndAssert(reader); IEnumerable cases = (IEnumerable)FSharpUtils.GetUnionCases(null, objectType, null); caseName = reader.Value.ToString(); foreach (object c in cases) { if ((string)FSharpUtils.GetUnionCaseInfoName(c) == caseName) { matchingCaseInfo = c; break; } } if (matchingCaseInfo == null) { throw JsonSerializationException.Create(reader, "No union type found with the name '{0}'.".FormatWith(CultureInfo.InvariantCulture, caseName)); } } else if (string.Equals(propertyName, FieldsPropertyName, StringComparison.OrdinalIgnoreCase)) { ReadAndAssert(reader); if (reader.TokenType != JsonToken.StartArray) { throw JsonSerializationException.Create(reader, "Union fields must been an array."); } fields = (JArray)JToken.ReadFrom(reader); } else { throw JsonSerializationException.Create(reader, "Unexpected property '{0}' found when reading union.".FormatWith(CultureInfo.InvariantCulture, propertyName)); } ReadAndAssert(reader); } if (matchingCaseInfo == null) { throw JsonSerializationException.Create(reader, "No '{0}' property with union name found.".FormatWith(CultureInfo.InvariantCulture, CasePropertyName)); } if (fields == null) { throw JsonSerializationException.Create(reader, "No '{0}' property with union fields found.".FormatWith(CultureInfo.InvariantCulture, FieldsPropertyName)); } PropertyInfo[] fieldProperties = (PropertyInfo[])FSharpUtils.GetUnionCaseInfoFields(matchingCaseInfo); if (fieldProperties.Length != fields.Count) { throw JsonSerializationException.Create(reader, "The number of field values does not match the number of properties definied by union '{0}'.".FormatWith(CultureInfo.InvariantCulture, caseName)); } object[] typedFieldValues = new object[fieldProperties.Length]; for (int i = 0; i < fields.Count; i++) { JToken t = fields[i]; PropertyInfo fieldProperty = fieldProperties[i]; typedFieldValues[i] = t.ToObject(fieldProperty.PropertyType); } return(FSharpUtils.MakeUnion(null, matchingCaseInfo, typedFieldValues, null)); }
/// <summary> /// Initializes a new instance of the <see cref="JsonArrayContract"/> class. /// </summary> /// <param name="underlyingType">The underlying type for the contract.</param> public JsonArrayContract(Type underlyingType) : base(underlyingType) { ContractType = JsonContractType.Array; IsArray = CreatedType.IsArray; bool canDeserialize; Type tempCollectionType; if (IsArray) { CollectionItemType = ReflectionUtils.GetCollectionItemType(UnderlyingType); IsReadOnlyOrFixedSize = true; _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType); canDeserialize = true; IsMultidimensionalArray = (IsArray && UnderlyingType.GetArrayRank() > 1); } else if (typeof(IList).IsAssignableFrom(underlyingType)) { if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType)) { CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0]; } else { CollectionItemType = ReflectionUtils.GetCollectionItemType(underlyingType); } if (underlyingType == typeof(IList)) { CreatedType = typeof(List <object>); } if (CollectionItemType != null) { _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType); } IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyCollection <>)); canDeserialize = true; } else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(ICollection <>), out _genericCollectionDefinitionType)) { CollectionItemType = _genericCollectionDefinitionType.GetGenericArguments()[0]; if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ICollection <>)) || ReflectionUtils.IsGenericDefinition(underlyingType, typeof(IList <>))) { CreatedType = typeof(List <>).MakeGenericType(CollectionItemType); } if (ReflectionUtils.IsGenericDefinition(underlyingType, typeof(ISet <>))) { CreatedType = typeof(HashSet <>).MakeGenericType(CollectionItemType); } _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType); canDeserialize = true; ShouldCreateWrapper = true; } else if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IEnumerable <>), out tempCollectionType)) { CollectionItemType = tempCollectionType.GetGenericArguments()[0]; if (ReflectionUtils.IsGenericDefinition(UnderlyingType, typeof(IEnumerable <>))) { CreatedType = typeof(List <>).MakeGenericType(CollectionItemType); } _parametrizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(underlyingType, CollectionItemType); #if !(NET35 || NET20 || NETFX_CORE) if (!HasParametrizedCreator && underlyingType.Name == FSharpUtils.FSharpListTypeName) { FSharpUtils.EnsureInitialized(underlyingType.Assembly()); _parametrizedCreator = FSharpUtils.CreateSeq(CollectionItemType); } #endif if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { _genericCollectionDefinitionType = tempCollectionType; IsReadOnlyOrFixedSize = false; ShouldCreateWrapper = false; canDeserialize = true; } else { _genericCollectionDefinitionType = typeof(List <>).MakeGenericType(CollectionItemType); IsReadOnlyOrFixedSize = true; ShouldCreateWrapper = true; canDeserialize = HasParametrizedCreator; } } else { // types that implement IEnumerable and nothing else canDeserialize = false; ShouldCreateWrapper = true; } CanDeserialize = canDeserialize; }
public JsonDictionaryContract(Type underlyingType) : base(underlyingType) { ContractType = JsonContractType.Dictionary; Type?keyType; Type?valueType; if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(IDictionary <,>), out _genericCollectionDefinitionType)) { keyType = _genericCollectionDefinitionType.GetGenericArguments()[0]; valueType = _genericCollectionDefinitionType.GetGenericArguments()[1]; if (ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(IDictionary <,>))) { CreatedType = typeof(Dictionary <,>).MakeGenericType(keyType, valueType); } else if (NonNullableUnderlyingType.IsGenericType()) { // ConcurrentDictionary<,> + IDictionary setter + null value = error // wrap to use generic setter // https://github.com/JamesNK/Newtonsoft.Json/issues/1582 var typeDefinition = NonNullableUnderlyingType.GetGenericTypeDefinition(); if (typeDefinition.FullName == JsonTypeReflector.ConcurrentDictionaryTypeName) { ShouldCreateWrapper = true; } } #if HAVE_READ_ONLY_COLLECTIONS IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(NonNullableUnderlyingType, typeof(ReadOnlyDictionary <,>)); #endif } #if HAVE_READ_ONLY_COLLECTIONS else if (ReflectionUtils.ImplementsGenericDefinition(NonNullableUnderlyingType, typeof(IReadOnlyDictionary <,>), out _genericCollectionDefinitionType)) { keyType = _genericCollectionDefinitionType.GetGenericArguments()[0]; valueType = _genericCollectionDefinitionType.GetGenericArguments()[1]; if (ReflectionUtils.IsGenericDefinition(NonNullableUnderlyingType, typeof(IReadOnlyDictionary <,>))) { CreatedType = typeof(ReadOnlyDictionary <,>).MakeGenericType(keyType, valueType); } IsReadOnlyOrFixedSize = true; } #endif else { ReflectionUtils.GetDictionaryKeyValueTypes(NonNullableUnderlyingType, out keyType, out valueType); if (NonNullableUnderlyingType == typeof(IDictionary)) { CreatedType = typeof(Dictionary <object, object>); } } if (keyType != null && valueType != null) { _parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor( CreatedType, typeof(KeyValuePair <,>).MakeGenericType(keyType, valueType), typeof(IDictionary <,>).MakeGenericType(keyType, valueType)); #if HAVE_FSHARP_TYPES if (!HasParameterizedCreatorInternal && NonNullableUnderlyingType.Name == FSharpUtils.FSharpMapTypeName) { FSharpUtils.EnsureInitialized(NonNullableUnderlyingType.Assembly()); _parameterizedCreator = FSharpUtils.Instance.CreateMap(keyType, valueType); } #endif } if (!typeof(IDictionary).IsAssignableFrom(CreatedType)) { ShouldCreateWrapper = true; } DictionaryKeyType = keyType; DictionaryValueType = valueType; #if (NET20 || NET35) if (DictionaryValueType != null && ReflectionUtils.IsNullableType(DictionaryValueType)) { // bug in .NET 2.0 & 3.5 that Dictionary<TKey, Nullable<TValue>> throws an error when adding null via IDictionary[key] = object // wrapper will handle calling Add(T) instead if (ReflectionUtils.InheritsGenericDefinition(CreatedType, typeof(Dictionary <,>), out _)) { ShouldCreateWrapper = true; } } #endif if (DictionaryKeyType != null && DictionaryValueType != null && ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract( NonNullableUnderlyingType, DictionaryKeyType, DictionaryValueType, out var immutableCreatedType, out var immutableParameterizedCreator)) { CreatedType = immutableCreatedType; _parameterizedCreator = immutableParameterizedCreator; IsReadOnlyOrFixedSize = true; } }
public JsonDictionaryContract(Type underlyingType) { Class6.yDnXvgqzyB5jw(); base(underlyingType); Type genericArguments; Type type; Type type1; ObjectConstructor <object> objectConstructor; this.ContractType = JsonContractType.Dictionary; if (ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IDictionary <,>), out this._genericCollectionDefinitionType)) { genericArguments = this._genericCollectionDefinitionType.GetGenericArguments()[0]; type = this._genericCollectionDefinitionType.GetGenericArguments()[1]; if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IDictionary <,>))) { base.CreatedType = typeof(Dictionary <,>).MakeGenericType(new Type[] { genericArguments, type }); } else if (underlyingType.IsGenericType() && underlyingType.GetGenericTypeDefinition().FullName == "System.Collections.Concurrent.ConcurrentDictionary`2") { this.ShouldCreateWrapper = true; } this.IsReadOnlyOrFixedSize = ReflectionUtils.InheritsGenericDefinition(underlyingType, typeof(ReadOnlyDictionary <,>)); } else if (!ReflectionUtils.ImplementsGenericDefinition(underlyingType, typeof(IReadOnlyDictionary <,>), out this._genericCollectionDefinitionType)) { ReflectionUtils.GetDictionaryKeyValueTypes(base.UnderlyingType, out genericArguments, out type); if (base.UnderlyingType == typeof(IDictionary)) { base.CreatedType = typeof(Dictionary <object, object>); } } else { genericArguments = this._genericCollectionDefinitionType.GetGenericArguments()[0]; type = this._genericCollectionDefinitionType.GetGenericArguments()[1]; if (ReflectionUtils.IsGenericDefinition(base.UnderlyingType, typeof(IReadOnlyDictionary <,>))) { base.CreatedType = typeof(ReadOnlyDictionary <,>).MakeGenericType(new Type[] { genericArguments, type }); } this.IsReadOnlyOrFixedSize = true; } if (genericArguments != null && type != null) { this._parameterizedConstructor = CollectionUtils.ResolveEnumerableCollectionConstructor(base.CreatedType, typeof(KeyValuePair <,>).MakeGenericType(new Type[] { genericArguments, type }), typeof(IDictionary <,>).MakeGenericType(new Type[] { genericArguments, type })); if (!this.HasParameterizedCreatorInternal && underlyingType.Name == "FSharpMap`2") { FSharpUtils.EnsureInitialized(underlyingType.Assembly()); this._parameterizedCreator = FSharpUtils.CreateMap(genericArguments, type); } } if (!typeof(IDictionary).IsAssignableFrom(base.CreatedType)) { this.ShouldCreateWrapper = true; } this.DictionaryKeyType = genericArguments; this.DictionaryValueType = type; if (ImmutableCollectionsUtils.TryBuildImmutableForDictionaryContract(underlyingType, this.DictionaryKeyType, this.DictionaryValueType, out type1, out objectConstructor)) { base.CreatedType = type1; this._parameterizedCreator = objectConstructor; this.IsReadOnlyOrFixedSize = true; } }