Ejemplo n.º 1
0
        protected void InternalReadXml(XmlReader r)
        {
            string containingElementType = "";

            MemberInfo[] fields = GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

            bool skipCloseElement = false;

            foreach (MemberInfo f in fields)
            {
                var    mInfo       = Globs.GetAttr <MarshalAsAttribute>(f);
                var    elementType = Globs.GetMemberType(f);
                string elementName = f.Name.TrimStart(new[] { '_' });

                if (r.HasAttributes)
                {
                    containingElementType = r.GetAttribute("type");
                }
                r.ReadStartElement(elementName);

                string s;
                Object val;
                switch (mInfo.MarshType)
                {
                case MarshalType.UnionSelector:
                    s = r.ReadContentAsString();
                    // ReSharper disable once AssignNullToNotNullAttribute
                    val = Enum.Parse(elementType, s);
                    Globs.SetMember(f, this, val);
                    break;

                case MarshalType.ArrayCount:
                    int intVal = r.ReadContentAsInt();
                    // ReSharper disable once AssignNullToNotNullAttribute
                    val = Convert.ChangeType(intVal, elementType);
                    Globs.SetMember(f, this, val);
                    break;

                case MarshalType.Normal:
                    // ReSharper disable once PossibleNullReferenceException
                    if (elementType.GetTypeInfo().IsSubclassOf(typeof(TpmStructureBase)))
                    {
                        val = Activator.CreateInstance(elementType);
                        Globs.SetMember(f, this, val);

                        ((TpmStructureBase)val).InternalReadXml(r);
                        break;
                    }
                    // ReSharper disable once RedundantIfElseBlock
                    else
                    {
                        if (elementType.TypeIsEnum())
                        {
                            s   = r.ReadContentAsString();
                            val = Enum.Parse(elementType, s);
                            Globs.SetMember(f, this, val);
                            break;
                        }
                        if (elementType == typeof(uint) || elementType == typeof(ushort) || elementType == typeof(byte))
                        {
                            // TODO: This should be unsigned
                            long longVal = r.ReadContentAsLong();
                            val = Convert.ChangeType(longVal, elementType);

                            Globs.SetMember(f, this, val);
                            break;
                        }
                        throw new NotImplementedException("");
                    }
                    // ReSharper disable once CSharpWarnings::CS0162
                    // ReSharper disable once HeuristicUnreachableCode
                    throw new NotImplementedException("");

                case MarshalType.FixedLengthArray:
                case MarshalType.SpecialVariableLengthArray:
                case MarshalType.VariableLengthArray:
                    var supportedElementaryTypes = new[] { typeof(byte[]), typeof(ushort[]), typeof(uint[]) };
                    if (supportedElementaryTypes.Contains(elementType))
                    {
                        if (r.HasValue)
                        {
                            // ReSharper disable once AssignNullToNotNullAttribute
                            val = r.ReadContentAs(elementType, null);
                            Globs.SetMember(f, this, val);
                        }
                        else
                        {
                            // ReSharper disable once AssignNullToNotNullAttribute
                            Object nullObj = Activator.CreateInstance(elementType, 0x0);
                            Globs.SetMember(f, this, nullObj);
                            skipCloseElement = true;
                        }
                        break;
                    }
                    // ReSharper disable once RedundantIfElseBlock
                    else
                    {
                        throw new NotImplementedException("");
                    }

                case MarshalType.Union:
                    // ReSharper disable once AssignNullToNotNullAttribute
                    val = Activator.CreateInstance(Type.GetType(containingElementType));
                    ((TpmStructureBase)val).InternalReadXml(r);
                    Globs.SetMember(f, this, val);
                    break;

                default:
                    throw new NotImplementedException("");
                }

                if (!skipCloseElement)
                {
                    r.ReadEndElement();
                }
                skipCloseElement = false;
            }
        }
Ejemplo n.º 2
0
        protected void InternalWriteXml(XmlWriter w)
        {
            MemberInfo[] fields = GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

            string thisObjectName = GetType().Name;

            foreach (MemberInfo f in fields)
            {
                var    mInfo = Globs.GetAttr <MarshalAsAttribute>(f);
                Object obj   = Globs.GetMember(f, this);

                string elementName = f.Name;
                elementName = elementName.TrimStart(new[] { '_' });
                w.WriteStartElement(elementName);
                switch (mInfo.MarshType)
                {
                case MarshalType.UnionSelector:
                case MarshalType.ArrayCount:
                    // ReSharper disable once PossibleNullReferenceException
                    w.WriteString(obj.ToString());
                    break;

                case MarshalType.Normal:
                    // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull
                    if (obj is TpmStructureBase)
                    {
                        //w.WriteStartElement(GetTypeName(obj));
                        ((TpmStructureBase)obj).InternalWriteXml(w);
                        //w.WriteEndElement();
                    }
                    else
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        w.WriteString(obj.ToString());
                    }
                    break;

                case MarshalType.FixedLengthArray:
                case MarshalType.SpecialVariableLengthArray:
                case MarshalType.VariableLengthArray:
                    // ReSharper disable once PossibleNullReferenceException
                    Type elementType = obj.GetType().GetElementType();
                    var  supportedElementaryTypes = new[] { typeof(byte), typeof(ushort), typeof(uint) };
                    if (supportedElementaryTypes.Contains(elementType))
                    {
                        w.WriteValue(obj);
                    }
                    else
                    {
                        // ReSharper disable once UnusedVariable
                        foreach (Object o in (Array)obj)
                        {
                            // ReSharper disable once PossibleInvalidCastException
                            ((TpmStructureBase)obj).InternalWriteXml(w);
                        }
                    }
                    break;

                case MarshalType.Union:
                    // ReSharper disable once PossibleNullReferenceException
                    string typeName = obj.GetType().ToString();
                    w.WriteAttributeString("type", typeName);

                    //w.WriteStartElement(GetTypeName(obj));
                    ((TpmStructureBase)obj).InternalWriteXml(w);
                    //w.WriteEndElement();
                    break;

                default:
                    throw new NotImplementedException("NOT IMPLEMENTED");
                }
                w.WriteEndElement();
            }
        }