Exemple #1
0
        // Generate the MSIL to retrieve a value for a particular field or property from a IJsonReader
        private static void GenerateGetJsonValue(JsonMemberInfo m, ILGenerator il)
        {
            Action <string> generateCallToHelper = helperName =>
            {
                // Call the helper
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Call, typeof(Emit).GetMethod(helperName, new Type[] { typeof(IJsonReader) }));

                // Move to next token
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Callvirt, typeof(IJsonReader).GetMethod("NextToken", new Type[] { }));
            };

            Type[] numericTypes = new Type[] {
                typeof(int), typeof(uint), typeof(long), typeof(ulong),
                typeof(short), typeof(ushort), typeof(decimal),
                typeof(byte), typeof(sbyte),
                typeof(double), typeof(float)
            };

            if (m.MemberType == typeof(string))
            {
                generateCallToHelper("GetLiteralString");
            }

            else if (m.MemberType == typeof(bool))
            {
                generateCallToHelper("GetLiteralBool");
            }

            else if (m.MemberType == typeof(char))
            {
                generateCallToHelper("GetLiteralChar");
            }

            else if (numericTypes.Contains(m.MemberType))
            {
                // Get raw number string
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Call, typeof(Emit).GetMethod("GetLiteralNumber", new Type[] { typeof(IJsonReader) }));

                // Convert to a string
                il.Emit(OpCodes.Call, typeof(CultureInfo).GetProperty("InvariantCulture").GetGetMethod());
                il.Emit(OpCodes.Call, m.MemberType.GetMethod("Parse", new Type[] { typeof(string), typeof(IFormatProvider) }));

                //
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Callvirt, typeof(IJsonReader).GetMethod("NextToken", new Type[] { }));
            }

            else
            {
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldtoken, m.MemberType);
                il.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", new Type[] { typeof(RuntimeTypeHandle) }));
                il.Emit(OpCodes.Callvirt, typeof(IJsonReader).GetMethod("Parse", new Type[] { typeof(Type) }));
                il.Emit(m.MemberType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, m.MemberType);
            }
        }
 bool FindMemberInfo(string name, out JsonMemberInfo found)
 {
     for (int i = 0; i < Members.Count; i++)
     {
         int index = (i + _lastFoundIndex) % Members.Count;
         var jmi   = Members[index];
         if (jmi.JsonKey == name)
         {
             _lastFoundIndex = index;
             found           = jmi;
             return(true);
         }
     }
     found = null;
     return(false);
 }