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);
        }
Exemplo n.º 2
0
        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);
        }
        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));
        }
        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);
        }
Exemplo n.º 5
0
        /// <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));
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
 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);
 }
Exemplo n.º 8
0
        /// <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));
        }
Exemplo n.º 9
0
        private static Type CreateUnionTypeLookup(Type t)
        {
            object obj = ((object[])FSharpUtils.GetUnionCases(null, new object[] { t, null })).First <object>();

            return((Type)FSharpUtils.GetUnionCaseInfoDeclaringType(obj));
        }