Пример #1
0
            private void StoreCollectionValue(LocalBuilder collection, LocalBuilder value, CollectionDataContract collectionContract)
            {
                if (collectionContract.Kind == CollectionKind.GenericDictionary || collectionContract.Kind == CollectionKind.Dictionary)
                {
                    ClassDataContract keyValuePairContract = DataContract.GetDataContract(value.LocalType) as ClassDataContract;
                    if (keyValuePairContract == null)
                    {
                        DiagnosticUtility.DebugAssert("Failed to create contract for KeyValuePair type");
                    }
                    DataMember   keyMember   = keyValuePairContract.Members[0];
                    DataMember   valueMember = keyValuePairContract.Members[1];
                    LocalBuilder pairKey     = _ilg.DeclareLocal(keyMember.MemberType, keyMember.Name);
                    LocalBuilder pairValue   = _ilg.DeclareLocal(valueMember.MemberType, valueMember.Name);
                    _ilg.LoadAddress(value);
                    _ilg.LoadMember(keyMember.MemberInfo);
                    _ilg.Stloc(pairKey);
                    _ilg.LoadAddress(value);
                    _ilg.LoadMember(valueMember.MemberInfo);
                    _ilg.Stloc(pairValue);

                    _ilg.Call(collection, collectionContract.AddMethod, pairKey, pairValue);
                    if (collectionContract.AddMethod.ReturnType != Globals.TypeOfVoid)
                    {
                        _ilg.Pop();
                    }
                }
                else
                {
                    _ilg.Call(collection, collectionContract.AddMethod, value);
                    if (collectionContract.AddMethod.ReturnType != Globals.TypeOfVoid)
                    {
                        _ilg.Pop();
                    }
                }
            }
Пример #2
0
            private void InitArgs(Type objType)
            {
                _xmlWriterArg    = _ilg.GetArg(0);
                _contextArg      = _ilg.GetArg(2);
                _dataContractArg = _ilg.GetArg(3);

                _objectLocal = _ilg.DeclareLocal(objType, "objSerialized");
                ArgBuilder objectArg = _ilg.GetArg(1);

                _ilg.Load(objectArg);

                // Copy the data from the DataTimeOffset object passed in to the DateTimeOffsetAdapter.
                // DateTimeOffsetAdapter is used here for serialization purposes to bypass the ISerializable implementation
                // on DateTimeOffset; which does not work in partial trust.

                if (objType == Globals.TypeOfDateTimeOffsetAdapter)
                {
                    _ilg.ConvertValue(objectArg.ArgType, Globals.TypeOfDateTimeOffset);
                    _ilg.Call(XmlFormatGeneratorStatics.GetDateTimeOffsetAdapterMethod);
                }
                //Copy the KeyValuePair<K,T> to a KeyValuePairAdapter<K,T>.
                else if (objType.IsGenericType && objType.GetGenericTypeDefinition() == Globals.TypeOfKeyValuePairAdapter)
                {
                    ClassDataContract dc = (ClassDataContract)DataContract.GetDataContract(objType);
                    _ilg.ConvertValue(objectArg.ArgType, Globals.TypeOfKeyValuePair.MakeGenericType(dc.KeyValuePairGenericArguments));
                    _ilg.New(dc.KeyValuePairAdapterConstructorInfo);
                }
                else
                {
                    _ilg.ConvertValue(objectArg.ArgType, objType);
                }
                _ilg.Stloc(_objectLocal);
            }
Пример #3
0
 private void StoreCollectionValue(LocalBuilder collection, LocalBuilder value, CollectionDataContract collectionContract)
 {
     if ((collectionContract.Kind == CollectionKind.GenericDictionary) || (collectionContract.Kind == CollectionKind.Dictionary))
     {
         ClassDataContract dataContract = DataContract.GetDataContract(value.LocalType) as ClassDataContract;
         DataMember        member       = dataContract.Members[0];
         DataMember        member2      = dataContract.Members[1];
         LocalBuilder      local        = this.ilg.DeclareLocal(member.MemberType, member.Name);
         LocalBuilder      builder2     = this.ilg.DeclareLocal(member2.MemberType, member2.Name);
         this.ilg.LoadAddress(value);
         this.ilg.LoadMember(member.MemberInfo);
         this.ilg.Stloc(local);
         this.ilg.LoadAddress(value);
         this.ilg.LoadMember(member2.MemberInfo);
         this.ilg.Stloc(builder2);
         this.ilg.Call(collection, collectionContract.AddMethod, local, builder2);
         if (collectionContract.AddMethod.ReturnType != Globals.TypeOfVoid)
         {
             this.ilg.Pop();
         }
     }
     else
     {
         this.ilg.Call(collection, collectionContract.AddMethod, value);
         if (collectionContract.AddMethod.ReturnType != Globals.TypeOfVoid)
         {
             this.ilg.Pop();
         }
     }
 }
Пример #4
0
        internal DataContract GetDataContract(Type clrType)
        {
            if (dataContractSurrogate == null)
            {
                return(DataContract.GetDataContract(clrType));
            }
            DataContract dataContract = DataContract.GetBuiltInDataContract(clrType);

            if (dataContract != null)
            {
                return(dataContract);
            }
            Type dcType = DataContractSurrogateCaller.GetDataContractType(dataContractSurrogate, clrType);

            //if (clrType.IsValueType != dcType.IsValueType)
            //    throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.ValueTypeMismatchInSurrogatedType, dcType, clrType)));
            dataContract = DataContract.GetDataContract(dcType);
            if (!SurrogateDataTable.Contains(dataContract))
            {
                object customData = DataContractSurrogateCaller.GetCustomDataToExport(
                    dataContractSurrogate, clrType, dcType);
                if (customData != null)
                {
                    SurrogateDataTable.Add(dataContract, customData);
                }
            }
            return(dataContract);
        }
Пример #5
0
        public override bool TryResolveType(Type type, Type declaredType, DataContractResolver knownTypeResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
        {
            if (type == null)
            {
                typeName      = null;
                typeNamespace = null;
                return(false);
            }
            if (declaredType != null && declaredType.IsInterface && CollectionDataContract.IsCollectionInterface(declaredType))
            {
                typeName      = null;
                typeNamespace = null;
                return(true);
            }

            DataContract contract = DataContract.GetDataContract(type);

            if (_context.IsKnownType(contract, contract.KnownDataContracts, declaredType))
            {
                typeName      = contract.Name;
                typeNamespace = contract.Namespace;
                return(true);
            }
            else
            {
                typeName      = null;
                typeNamespace = null;
                return(false);
            }
        }
 internal virtual DataContract GetDataContract(RuntimeTypeHandle typeHandle, Type type)
 {
     if (this.IsGetOnlyCollection)
     {
         return(DataContract.GetGetOnlyCollectionDataContract(DataContract.GetId(typeHandle), typeHandle, type, this.Mode));
     }
     return(DataContract.GetDataContract(typeHandle, type, this.Mode));
 }
 internal virtual DataContract GetDataContract(int id, RuntimeTypeHandle typeHandle)
 {
     if (this.IsGetOnlyCollection)
     {
         return(DataContract.GetGetOnlyCollectionDataContract(id, typeHandle, null, this.Mode));
     }
     return(DataContract.GetDataContract(id, typeHandle, this.Mode));
 }
 internal virtual DataContract GetDataContract(RuntimeTypeHandle typeHandle, Type?type)
 {
     if (IsGetOnlyCollection)
     {
         return(DataContract.GetGetOnlyCollectionDataContract(DataContract.GetId(typeHandle), typeHandle, type));
     }
     else
     {
         return(DataContract.GetDataContract(typeHandle, type));
     }
 }
 internal virtual DataContract GetDataContract(int id, RuntimeTypeHandle typeHandle)
 {
     if (IsGetOnlyCollection)
     {
         return(DataContract.GetGetOnlyCollectionDataContract(id, typeHandle, null /*type*/));
     }
     else
     {
         return(DataContract.GetDataContract(id, typeHandle));
     }
 }
 internal static DataContract GetDataContract(DataContract declaredTypeContract, Type declaredType, Type objectType)
 {
     if (declaredType.IsInterface && CollectionDataContract.IsCollectionInterface(declaredType))
     {
         return(declaredTypeContract);
     }
     if (declaredType.IsArray)
     {
         return(declaredTypeContract);
     }
     return(DataContract.GetDataContract(objectType.TypeHandle, objectType, SerializationMode.SharedContract));
 }
        internal static DataContract GetDataContractFromSurrogateSelector(ISurrogateSelector surrogateSelector, StreamingContext context, RuntimeTypeHandle typeHandle, Type type, ref Hashtable surrogateDataContracts)
        {
            if (surrogateSelector == null)
            {
                return(null);
            }

            if (type == null)
            {
                type = Type.GetTypeFromHandle(typeHandle);
            }
            DataContract builtInDataContract = DataContract.GetBuiltInDataContract(type);

            if (builtInDataContract != null)
            {
                return(builtInDataContract);
            }
            if (surrogateDataContracts != null)
            {
                DataContract cachedSurrogateContract = (DataContract)surrogateDataContracts[type];
                if (cachedSurrogateContract != null)
                {
                    return(cachedSurrogateContract);
                }
            }
            DataContract            surrogateContract = null;
            ISerializationSurrogate surrogate         = GetSurrogate(type, surrogateSelector, context);

            if (surrogate != null)
            {
                surrogateContract = new SurrogateDataContract(type, surrogate);
            }
            else if (type.IsArray)
            {
                Type         elementType  = type.GetElementType();
                DataContract itemContract = GetDataContractFromSurrogateSelector(surrogateSelector, context, elementType.TypeHandle, elementType, ref surrogateDataContracts);
                if (itemContract == null)
                {
                    itemContract = DataContract.GetDataContract(elementType.TypeHandle, elementType, SerializationMode.SharedType);
                }
                surrogateContract = new CollectionDataContract(type, itemContract);
            }
            if (surrogateContract != null)
            {
                if (surrogateDataContracts == null)
                {
                    surrogateDataContracts = new Hashtable();
                }
                surrogateDataContracts.Add(type, surrogateContract);
                return(surrogateContract);
            }
            return(null);
        }
Пример #12
0
        public XmlQualifiedName?GetRootElementName(Type type)
        {
            ArgumentNullException.ThrowIfNull(type);

            type = GetSurrogatedType(type);
            DataContract dataContract = DataContract.GetDataContract(type);

            EnsureTypeNotGeneric(dataContract.UnderlyingType);
            if (dataContract is not XmlDataContract xdc || xdc.HasRoot) // All non-XmlDataContracts "have root".
            {
                return(new XmlQualifiedName(dataContract.TopLevelElementName !.Value, dataContract.TopLevelElementNamespace !.Value));
            }
            private void StoreCollectionValue(LocalBuilder collection, LocalBuilder value, CollectionDataContract collectionContract)
            {
                Debug.Assert(collectionContract.AddMethod != null);

                if (collectionContract.Kind == CollectionKind.GenericDictionary || collectionContract.Kind == CollectionKind.Dictionary)
                {
                    ClassDataContract?keyValuePairContract = DataContract.GetDataContract(value.LocalType) as ClassDataContract;
                    if (keyValuePairContract == null)
                    {
                        DiagnosticUtility.DebugAssert("Failed to create contract for KeyValuePair type");
                    }
                    DataMember   keyMember   = keyValuePairContract.Members ![0];
Пример #14
0
        public XmlSchemaType?GetSchemaType(Type type)
        {
            ArgumentNullException.ThrowIfNull(type);

            type = GetSurrogatedType(type);
            DataContract dataContract = DataContract.GetDataContract(type);

            EnsureTypeNotGeneric(dataContract.UnderlyingType);
            if (dataContract is XmlDataContract xmlDataContract && xmlDataContract.IsAnonymous)
            {
                return(xmlDataContract.XsdType);
            }
            return(null);
        }
Пример #15
0
        public XmlQualifiedName GetSchemaTypeName(Type type)
        {
            ArgumentNullException.ThrowIfNull(type);

            type = GetSurrogatedType(type);
            DataContract dataContract = DataContract.GetDataContract(type);

            EnsureTypeNotGeneric(dataContract.UnderlyingType);
            if (dataContract is XmlDataContract xmlDataContract && xmlDataContract.IsAnonymous)
            {
                return(XmlQualifiedName.Empty);
            }
            return(dataContract.XmlName);
        }
Пример #16
0
 internal static DataContract GetDataContract(DataContract declaredTypeContract, Type declaredType, Type objectType)
 {
     if (declaredType.IsInterface && CollectionDataContract.IsCollectionInterface(declaredType))
     {
         return(declaredTypeContract);
     }
     else if (declaredType.IsArray)//Array covariance is not supported in XSD
     {
         return(declaredTypeContract);
     }
     else
     {
         return(DataContract.GetDataContract(objectType.TypeHandle, objectType));
     }
 }
        public XmlQualifiedName GetRootElementName(Type type)
        {
            if (type == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type"));
            }
            type = this.GetSurrogatedType(type);
            DataContract dataContract = DataContract.GetDataContract(type);

            System.Runtime.Serialization.DataContractSet.EnsureTypeNotGeneric(dataContract.UnderlyingType);
            if (dataContract.HasRoot)
            {
                return(new XmlQualifiedName(dataContract.TopLevelElementName.Value, dataContract.TopLevelElementNamespace.Value));
            }
            return(null);
        }
Пример #18
0
        public XmlQualifiedName GetSchemaTypeName(Type type)
        {
            ArgumentNullException.ThrowIfNull(type);

            type = GetSurrogatedType(type);
            DataContract dataContract = DataContract.GetDataContract(type);

            DataContractSet.EnsureTypeNotGeneric(dataContract.UnderlyingType);
            XmlDataContract?xmlDataContract = dataContract as XmlDataContract;

            if (xmlDataContract != null && xmlDataContract.IsAnonymous)
            {
                return(XmlQualifiedName.Empty);
            }
            return(dataContract.StableName);
        }
Пример #19
0
            public ExceptionDataContractCriticalHelper(Type type)
                : base(type)
            {
                this.StableName = DataContract.GetStableName(type, out _hasDataContract);
                Type baseType = Globals.TypeOfException;

                this.IsValueType = type.GetTypeInfo().IsValueType;
                if (baseType != null && baseType != Globals.TypeOfObject && type != Globals.TypeOfException)
                {
                    DataContract baseContract = DataContract.GetDataContract(baseType);
                    this.BaseContract = baseContract as ExceptionDataContract;
                }
                else
                {
                    this.BaseContract = null;
                }

                ImportDataMembers();
                ImportKnownTypes();
                XmlDictionary dictionary = new XmlDictionary(2 + Members.Count);

                Name      = dictionary.Add(StableName.Name);
                Namespace = dictionary.Add(StableName.Namespace);

                int baseContractCount = 0;

                if (BaseContract == null)
                {
                    _memberNames        = new XmlDictionaryString[Members.Count];
                    _memberNamespaces   = new XmlDictionaryString[Members.Count];
                    _contractNamespaces = new XmlDictionaryString[1];
                }
                else
                {
                    _memberNames        = new XmlDictionaryString[Members.Count];
                    _memberNamespaces   = new XmlDictionaryString[Members.Count];
                    baseContractCount   = BaseContract._contractNamespaces.Length;
                    _contractNamespaces = new XmlDictionaryString[1 + baseContractCount];
                    Array.Copy(BaseContract._contractNamespaces, _contractNamespaces, baseContractCount);
                }
                _contractNamespaces[baseContractCount] = Namespace;
                for (int i = 0; i < Members.Count; i++)
                {
                    _memberNames[i]      = dictionary.Add(Members[i].Name);
                    _memberNamespaces[i] = Namespace;
                }
            }
Пример #20
0
        public XmlQualifiedName?GetRootElementName(Type type)
        {
            ArgumentNullException.ThrowIfNull(type);

            type = GetSurrogatedType(type);
            DataContract dataContract = DataContract.GetDataContract(type);

            DataContractSet.EnsureTypeNotGeneric(dataContract.UnderlyingType);
            if (dataContract.HasRoot)
            {
                return(new XmlQualifiedName(dataContract.TopLevelElementName !.Value, dataContract.TopLevelElementNamespace !.Value));
            }
            else
            {
                return(null);
            }
        }
        public XmlSchemaType GetSchemaType(Type type)
        {
            if (type == null)
            {
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(type)));
            }
            type = GetSurrogatedType(type);
            DataContract dataContract = DataContract.GetDataContract(type);

            DataContractSet.EnsureTypeNotGeneric(dataContract.UnderlyingType);
            XmlDataContract xmlDataContract = dataContract as XmlDataContract;

            if (xmlDataContract != null && xmlDataContract.IsAnonymous)
            {
                return(xmlDataContract.XsdType);
            }
            return(null);
        }
        public XmlQualifiedName GetSchemaTypeName(Type type)
        {
            if (type == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type"));
            }
            type = this.GetSurrogatedType(type);
            DataContract dataContract = DataContract.GetDataContract(type);

            System.Runtime.Serialization.DataContractSet.EnsureTypeNotGeneric(dataContract.UnderlyingType);
            XmlDataContract contract2 = dataContract as XmlDataContract;

            if ((contract2 != null) && contract2.IsAnonymous)
            {
                return(XmlQualifiedName.Empty);
            }
            return(dataContract.StableName);
        }
        void StoreCollectionValue(object collection, Type valueType, object value, CollectionDataContract collectionContract)
        {
            if (collectionContract.Kind == CollectionKind.GenericDictionary || collectionContract.Kind == CollectionKind.Dictionary)
            {
                ClassDataContract keyValuePairContract = DataContract.GetDataContract(valueType) as ClassDataContract;
                if (keyValuePairContract == null)
                {
                    Fx.Assert("Failed to create contract for KeyValuePair type");
                }
                DataMember keyMember   = keyValuePairContract.Members [0];
                DataMember valueMember = keyValuePairContract.Members [1];
                object     pkey        = CodeInterpreter.GetMember(keyMember.MemberInfo, value);
                object     pvalue      = CodeInterpreter.GetMember(valueMember.MemberInfo, value);

                collectionContract.AddMethod.Invoke(collection, new object [] { pkey, pvalue });
            }
            else
            {
                collectionContract.AddMethod.Invoke(collection, new object [] { value });
            }
        }
        internal DataContract GetDataContract(RuntimeTypeHandle typeHandle, Type type, ref Hashtable surrogateDataContracts)
        {
            DataContract contract = GetDataContractFromSurrogateSelector(this.surrogateSelector, this.Context, typeHandle, type, ref surrogateDataContracts);

            if (contract != null)
            {
                return(contract);
            }
            if (this.cachedDataContract == null)
            {
                contract = DataContract.GetDataContract(typeHandle, type, SerializationMode.SharedType);
                this.cachedDataContract = contract;
                return(contract);
            }
            DataContract cachedDataContract = this.cachedDataContract;

            if (cachedDataContract.UnderlyingType.TypeHandle.Equals(typeHandle))
            {
                return(cachedDataContract);
            }
            return(DataContract.GetDataContract(typeHandle, type, SerializationMode.SharedType));
        }
        internal DataContract GetDataContract(Type clrType)
        {
            if (this.dataContractSurrogate == null)
            {
                return(DataContract.GetDataContract(clrType));
            }
            DataContract builtInDataContract = DataContract.GetBuiltInDataContract(clrType);

            if (builtInDataContract == null)
            {
                Type dataContractType = DataContractSurrogateCaller.GetDataContractType(this.dataContractSurrogate, clrType);
                builtInDataContract = DataContract.GetDataContract(dataContractType);
                if (!this.SurrogateDataTable.Contains(builtInDataContract))
                {
                    object obj2 = DataContractSurrogateCaller.GetCustomDataToExport(this.dataContractSurrogate, clrType, dataContractType);
                    if (obj2 != null)
                    {
                        this.SurrogateDataTable.Add(builtInDataContract, obj2);
                    }
                }
            }
            return(builtInDataContract);
        }
Пример #26
0
        internal DataContract GetDataContract(Type clrType)
        {
#if SUPPORT_SURROGATE
            if (_dataContractSurrogate == null)
            {
                return(DataContract.GetDataContract(clrType));
            }
#endif
            DataContract?dataContract = DataContract.GetBuiltInDataContract(clrType);
            if (dataContract != null)
            {
                return(dataContract);
            }

#if SUPPORT_SURROGATE
            Type dcType = DataContractSurrogateCaller.GetDataContractType(_dataContractSurrogate, clrType);
            if (clrType.IsValueType != dcType.IsValueType)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.ValueTypeMismatchInSurrogatedType, dcType, clrType)));
            }
#endif
            Type dcType = clrType;
            dataContract = DataContract.GetDataContract(dcType);
#if SUPPORT_SURROGATE
            if (!SurrogateDataTable.Contains(dataContract))
            {
                object customData = DataContractSurrogateCaller.GetCustomDataToExport(
                    _dataContractSurrogate, clrType, dcType);
                if (customData != null)
                {
                    SurrogateDataTable.Add(dataContract, customData);
                }
            }
#endif
            return(dataContract);
        }
Пример #27
0
        protected virtual void SerializeWithXsiType(XmlWriterDelegator xmlWriter, object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, int declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
        {
            bool verifyKnownType = false;

#if !NET_NATIVE
            DataContract dataContract;
            if (declaredType.GetTypeInfo().IsInterface&& CollectionDataContract.IsCollectionInterface(declaredType))
            {
                dataContract = GetDataContractSkipValidation(DataContract.GetId(objectTypeHandle), objectTypeHandle, objectType);
                if (OnHandleIsReference(xmlWriter, dataContract, obj))
                {
                    return;
                }
                dataContract = GetDataContract(declaredTypeHandle, declaredType);
#else
            DataContract dataContract = DataContract.GetDataContract(declaredType);
            if (dataContract.TypeIsInterface && dataContract.TypeIsCollectionInterface)
            {
                if (OnHandleIsReference(xmlWriter, dataContract, obj))
                {
                    return;
                }
                if (this.Mode == SerializationMode.SharedType && dataContract.IsValidContract(this.Mode))
                {
                    dataContract = dataContract.GetValidContract(this.Mode);
                }
                else
                {
                    dataContract = GetDataContract(declaredTypeHandle, declaredType);
                }
#endif
                if (!WriteClrTypeInfo(xmlWriter, dataContract) && DataContractResolver != null)
                {
                    if (objectType == null)
                    {
                        objectType = Type.GetTypeFromHandle(objectTypeHandle);
                    }
                    WriteResolvedTypeInfo(xmlWriter, objectType, declaredType);
                }
            }
            else if (declaredType.IsArray)//Array covariance is not supported in XSD. If declared type is array do not write xsi:type. Instead write xsi:type for each item
            {
                // A call to OnHandleIsReference is not necessary here -- arrays cannot be IsReference
                dataContract = GetDataContract(objectTypeHandle, objectType);
                WriteClrTypeInfo(xmlWriter, dataContract);
                dataContract = GetDataContract(declaredTypeHandle, declaredType);
            }
            else
            {
                dataContract = GetDataContract(objectTypeHandle, objectType);
                if (OnHandleIsReference(xmlWriter, dataContract, obj))
                {
                    return;
                }
                if (!WriteClrTypeInfo(xmlWriter, dataContract))
                {
                    DataContract declaredTypeContract = (declaredTypeID >= 0)
                        ? GetDataContract(declaredTypeID, declaredTypeHandle)
                        : GetDataContract(declaredTypeHandle, declaredType);
                    verifyKnownType = WriteTypeInfo(xmlWriter, dataContract, declaredTypeContract);
                }
            }

            SerializeAndVerifyType(dataContract, xmlWriter, obj, verifyKnownType, declaredTypeHandle, declaredType);
        }
Пример #28
0
        void WriteValue(Type memberType, object memberValue, bool writeXsiType)
        {
            Pointer memberValueRefPointer = null;

            if (memberType.IsPointer)
            {
                memberValueRefPointer = (Pointer)XmlFormatGeneratorStatics.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 && !writeXsiType)
                {
                    primitiveContract.XmlFormatContentWriterMethod.Invoke(writer, new object [] { memberValue });
                }
                else
                {
                    // InternalSerialize(XmlFormatGeneratorStatics.InternalSerializeMethod, () => memberValue, memberType, writeXsiType);
                    var typeHandleValue = Type.GetTypeHandle(memberValue);
                    var isDeclaredType  = typeHandleValue.Equals(CodeInterpreter.ConvertValue(memberValue, memberType, Globals.TypeOfObject));

                    ctx.InternalSerialize(writer, memberValue, isDeclaredType, writeXsiType, DataContract.GetId(memberType.TypeHandle), memberType.TypeHandle);
                }
            }
            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(ctx, new object [] { writer, memberType, DataContract.IsTypeSerializable(memberType) });
                }
                else
                {
                    PrimitiveDataContract primitiveContract = PrimitiveDataContract.GetPrimitiveDataContract(memberType);
                    if (primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject && !writeXsiType)
                    {
                        if (isNullableOfT)
                        {
                            primitiveContract.XmlFormatContentWriterMethod.Invoke(writer, new object [] { memberValue });
                        }
                        else
                        {
                            primitiveContract.XmlFormatContentWriterMethod.Invoke(ctx, 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(ctx, new object [] { writer, memberType, DataContract.IsTypeSerializable(memberType) });
                        }
                        else
                        {
                            var typeHandleValue = Type.GetTypeHandle(memberValue);
                            var isDeclaredType  = typeHandleValue.Equals(CodeInterpreter.ConvertValue(memberValue, memberType, Globals.TypeOfObject));
                            if (isNullableOfT)
                            {
                                ctx.InternalSerialize(writer, memberValue, isDeclaredType, writeXsiType, DataContract.GetId(memberType.TypeHandle), memberType.TypeHandle);
                            }
                            else if (memberType == Globals.TypeOfObject)
                            {
                                var dataContract = DataContract.GetDataContract(memberValue.GetType());
                                writer.WriteAttributeQualifiedName(Globals.XsiPrefix, DictionaryGlobals.XsiTypeLocalName, DictionaryGlobals.SchemaInstanceNamespace, dataContract.Name, dataContract.Namespace);
                                ctx.InternalSerializeReference(writer, memberValue, false, false, -1, typeHandleValue);
                            }
                            else
                            {
                                ctx.InternalSerializeReference(writer, memberValue, isDeclaredType, writeXsiType, DataContract.GetId(memberType.TypeHandle), memberType.TypeHandle);
                            }
                        }
                    }
                }
            }
        }