void CreateObject(ClassDataContract classContract)
        {
            Type type = objectType = classContract.UnderlyingType;

            if (type.IsValueType && !classContract.IsNonAttributedType)
            {
                type = Globals.TypeOfValueType;
            }

            if (classContract.UnderlyingType == Globals.TypeOfDBNull)
            {
                objectLocal = DBNull.Value;
            }
            else if (classContract.IsNonAttributedType)
            {
                if (type.IsValueType)
                {
                    objectLocal = FormatterServices.GetUninitializedObject(type);
                }
                else
                {
                    objectLocal = classContract.GetNonAttributedTypeConstructor().Invoke(new object [0]);
                }
            }
            else
            {
                objectLocal = CodeInterpreter.ConvertValue(XmlFormatReaderGenerator.UnsafeGetUninitializedObject(DataContract.GetIdForInitialization(classContract)), Globals.TypeOfObject, type);
            }
        }
 bool InvokeFactoryMethod(ClassDataContract classContract)
 {
     if (HasFactoryMethod(classContract))
     {
         objectLocal = CodeInterpreter.ConvertValue(context.GetRealObject((IObjectReference)objectLocal, Globals.NewObjectId), Globals.TypeOfObject, classContract.UnderlyingType);
         return(true);
     }
     return(false);
 }
 void InitArgs(Type objType)
 {
     if (objType == Globals.TypeOfDateTimeOffsetAdapter)
     {
         objLocal = DateTimeOffsetAdapter.GetDateTimeOffsetAdapter((DateTimeOffset)obj);
     }
     else
     {
         objLocal = CodeInterpreter.ConvertValue(obj, typeof(object), objType);
     }
 }
 object ReadCollectionItem(CollectionDataContract collectionContract, Type itemType)
 {
     if (collectionContract.Kind == CollectionKind.Dictionary || collectionContract.Kind == CollectionKind.GenericDictionary)
     {
         context.ResetAttributes();
         var revisedContract = XmlObjectSerializerWriteContextComplexJson.GetRevisedItemContract(collectionContract.ItemContract);
         var v = DataContractJsonSerializer.ReadJsonValue(revisedContract, xmlReader, context);
         return(CodeInterpreter.ConvertValue(v, Globals.TypeOfObject, itemType));
     }
     else
     {
         return(ReadValue(itemType, JsonGlobals.itemString));
     }
 }
        object InternalDeserialize(Type type, string name)
        {
            Type declaredType = type.IsPointer ? Globals.TypeOfReflectionPointer : type;
            var  obj          = context.InternalDeserialize(xmlReader, DataContract.GetId(declaredType.TypeHandle), declaredType.TypeHandle, name, string.Empty);

            if (type.IsPointer)
            {
                // wow, there is no way to convert void* to object in strongly typed way...
                return(JsonFormatGeneratorStatics.UnboxPointer.Invoke(null, new object [] { obj }));
            }
            else
            {
                return(CodeInterpreter.ConvertValue(obj, Globals.TypeOfObject, type));
            }
        }
        void InternalSerialize(MethodInfo methodInfo, Func <object> memberValue, Type memberType, bool writeXsiType)
        {
            var v = memberValue();
            var typeHandleValue = Type.GetTypeHandle(v);
            var isDeclaredType  = typeHandleValue.Equals(CodeInterpreter.ConvertValue(v, memberType, Globals.TypeOfObject));

            try {
                methodInfo.Invoke(context, new object [] { writer, memberValue != null ? v : null, isDeclaredType, writeXsiType, DataContract.GetId(memberType.TypeHandle), memberType.TypeHandle });
            } catch (TargetInvocationException ex) {
                if (ex.InnerException != null)
                {
                    throw ex.InnerException;
                }
                else
                {
                    throw;
                }
            }
        }
        object ReadValue(Type type, string name)
        {
            var    valueType = type;
            object value     = null;
            bool   shouldAssignNullableValue = false;
            int    nullables = 0;

            while (type.IsGenericType && type.GetGenericTypeDefinition() == Globals.TypeOfNullable)
            {
                nullables++;
                type = type.GetGenericArguments() [0];
            }

            PrimitiveDataContract primitiveContract = PrimitiveDataContract.GetPrimitiveDataContract(type);

            if ((primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject) || nullables != 0 || type.IsValueType)
            {
                context.ReadAttributes(xmlReader);
                string objectId = context.ReadIfNullOrRef(xmlReader, type, DataContract.IsTypeSerializable(type));
                // Deserialize null
                if (objectId == Globals.NullObjectId)
                {
                    if (nullables != 0)
                    {
                        value = Activator.CreateInstance(valueType);
                    }
                    else if (type.IsValueType)
                    {
                        throw new SerializationException(SR.GetString(SR.ValueTypeCannotBeNull, DataContract.GetClrTypeFullName(type)));
                    }
                    else
                    {
                        value = null;
                    }
                }
                else if (objectId == string.Empty)
                {
                    // Deserialize value

                    // Compare against Globals.NewObjectId, which is set to string.Empty

                    objectId = context.GetObjectId();

                    if (type.IsValueType)
                    {
                        if (!string.IsNullOrEmpty(objectId))
                        {
                            throw new SerializationException(SR.GetString(SR.ValueTypeCannotHaveId, DataContract.GetClrTypeFullName(type)));
                        }
                    }
                    object innerValueRead = null;
                    if (nullables != 0)
                    {
                        shouldAssignNullableValue = true;
                    }

                    if (primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject)
                    {
                        value = primitiveContract.XmlFormatReaderMethod.Invoke(xmlReader, new object [0]);
                        if (!type.IsValueType)
                        {
                            context.AddNewObject(value);
                        }
                    }
                    else
                    {
                        value = InternalDeserialize(type, name);
                    }
                }
                else
                {
                    // Deserialize ref
                    if (type.IsValueType)
                    {
                        throw new SerializationException(SR.GetString(SR.ValueTypeCannotHaveRef, DataContract.GetClrTypeFullName(type)));
                    }
                    else
                    {
                        value = CodeInterpreter.ConvertValue(context.GetExistingObject(objectId, type, name, string.Empty), Globals.TypeOfObject, type);
                    }
                }

                if (shouldAssignNullableValue)
                {
                    if (objectId != Globals.NullObjectId)
                    {
                        value = WrapNullableObject(type, value, valueType, nullables);
                    }
                }
            }
            else
            {
                value = InternalDeserialize(type, name);
            }

            return(value);
        }
        void WriteValue(Type memberType, object memberValue)
        {
            Pointer memberValueRefPointer = null;

            if (memberType.IsPointer)
            {
                memberValueRefPointer = (Pointer)JsonFormatGeneratorStatics.BoxPointer.Invoke(null, new object [] { memberValue, memberType });
            }
            bool isNullableOfT = (memberType.IsGenericType &&
                                  memberType.GetGenericTypeDefinition() == Globals.TypeOfNullable);

            if (memberType.IsValueType && !isNullableOfT)
            {
                PrimitiveDataContract primitiveContract = PrimitiveDataContract.GetPrimitiveDataContract(memberType);
                if (primitiveContract != null)
                {
                    primitiveContract.XmlFormatContentWriterMethod.Invoke(writer, new object [] { memberValue });
                }
                else
                {
                    InternalSerialize(XmlFormatGeneratorStatics.InternalSerializeMethod, () => memberValue, memberType, false);
                }
            }
            else
            {
                bool isNull;
                if (isNullableOfT)
                {
                    memberValue = UnwrapNullableObject(() => memberValue, ref memberType, out isNull);                     //Leaves !HasValue on stack
                }
                else
                {
                    isNull = memberValue == null;
                }
                if (isNull)
                {
                    XmlFormatGeneratorStatics.WriteNullMethod.Invoke(context, new object [] { writer, memberType, DataContract.IsTypeSerializable(memberType) });
                }
                else
                {
                    PrimitiveDataContract primitiveContract = PrimitiveDataContract.GetPrimitiveDataContract(memberType);
                    if (primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject)
                    {
                        if (isNullableOfT)
                        {
                            primitiveContract.XmlFormatContentWriterMethod.Invoke(writer, new object [] { memberValue });
                        }
                        else
                        {
                            primitiveContract.XmlFormatContentWriterMethod.Invoke(context, new object [] { writer, memberValue });
                        }
                    }
                    else
                    {
                        bool isNull2 = false;
                        if (memberType == Globals.TypeOfObject ||                         //boxed Nullable<T>
                            memberType == Globals.TypeOfValueType ||
                            ((IList)Globals.TypeOfNullable.GetInterfaces()).Contains(memberType))
                        {
                            var unwrappedMemberValue = CodeInterpreter.ConvertValue(memberValue, memberType.GetType(), Globals.TypeOfObject);
                            memberValue = unwrappedMemberValue;
                            isNull2     = memberValue == null;
                        }
                        if (isNull2)
                        {
                            XmlFormatGeneratorStatics.WriteNullMethod.Invoke(context, new object [] { writer, memberType, DataContract.IsTypeSerializable(memberType) });
                        }
                        else
                        {
                            InternalSerialize((isNullableOfT ? XmlFormatGeneratorStatics.InternalSerializeMethod : XmlFormatGeneratorStatics.InternalSerializeReferenceMethod),
                                              () => memberValue, memberType, false);
                        }
                    }
                }
            }
        }