Пример #1
0
            public void GenerateDeserialize(ILGenerator il, ILDeserializeMethodContainer ilMethods)
            {
                var local  = il.DeclareLocal(typeof(object));
                var isntIt = il.DefineLabel();

                // object objVal = array[i];
                ilMethods.LoadCurrentObject();
                il.EmitSetLocalVariable(local);

                // if (!(objVal is T))
                il.EmitLoadLocalVariable(local);
                il.EmitIsInstance <T>();
                il.EmitShortBranchTrue(isntIt);

                // if it's not a value type, we can check if it's null (since reference types can be null)
                if (!_valueType)
                {
                    // || objVal == null))
                    il.EmitLoadLocalVariable(local);
                    il.EmitShortBranchFalse(isntIt);
                }

                // return false;
                il.EmitConstantInt(0);
                il.EmitReturn();

                il.MarkLabel(isntIt);

                // i++;
                ilMethods.AddToIndex(() => il.EmitConstantInt(1));

                // result.Property = (T)objVal;
                ilMethods.SetMemberValue(() =>
                {
                    il.EmitLoadLocalVariable(local);
                });
            }
Пример #2
0
            public void GenerateDeserialize(ILGenerator il, ILDeserializeMethodContainer ilMethods)
            {
                var local  = il.DeclareLocal(typeof(object));
                var isInst = il.DeclareLocal(typeof(T));

                ilMethods.LoadCurrentObject();
                il.EmitSetLocalVariable(local);

                if (_valueType)
                {
                    // value type

                    /*
                     * object local = array[i];
                     *
                     * if (local is T isBranchSet)
                     * {
                     *     result.Member = isBranchSet;
                     * }
                     */

                    var ifCompleted = il.DefineLabel();

                    // if (local is T obj4)
                    il.EmitLoadLocalVariable(local);
                    il.EmitIsInstance <T>();
                    il.EmitShortBranchFalse(ifCompleted);

                    // result.Member = (int)isBranchSet;
                    ilMethods.SetMemberValue(() =>
                    {
                        il.EmitLoadLocalVariable(local);
                    });

                    il.MarkLabel(ifCompleted);

                    // i++;
                    ilMethods.AddToIndex(() => il.EmitConstantInt(1));
                }
                else
                {
                    // reference type

                    /*
                     * object local = array[i];
                     *
                     * if (local is T isBranchSet)
                     * {
                     *     result.Member = isBranchSet;
                     * }
                     * else if (local == null)
                     * {
                     *     result.Member = null;
                     * }
                     */

                    var ifCompleted  = il.DefineLabel();
                    var secondBranch = il.DefineLabel();

                    // if (local is T isInst)
                    il.EmitLoadLocalVariable(local);
                    il.EmitIsInstance <T>();
                    il.EmitDuplicate();
                    il.EmitSetLocalVariable(isInst);
                    il.EmitShortBranchFalse(secondBranch);

                    // result.Member = isInst;
                    ilMethods.SetMemberValue(() =>
                    {
                        il.EmitLoadLocalVariable(isInst);
                    });

                    il.EmitBranch(ifCompleted);

                    il.MarkLabel(secondBranch);

                    // else if (local == null)
                    il.EmitLoadLocalVariable(local);
                    il.EmitShortBranchTrue(ifCompleted);

                    // result.Member = null;
                    ilMethods.SetMemberValue(() =>
                    {
                        il.EmitLoadNull();
                    });

                    // }
                    il.MarkLabel(ifCompleted);

                    ilMethods.AddToIndex(() => il.EmitConstantInt(1));
                }
            }