Пример #1
0
        private static IValueItem AutoCreateMissingDeserializeType(uint expectedTypeId, string orgAssemblyName, string typeFullName, IStreamReader reader, ISerializeContext ctx)
        {
            //todo: restrict property count and global auto create types
            short propertyCount = reader.ReadInt16();

            string typeName = typeFullName.Split('.').Last();

            string assemblyName = $"IOCTalk.AutoGenerated.{orgAssemblyName}";

            StringBuilder code = new StringBuilder();

            code.AppendLine("using System;");
            code.AppendLine();
            code.Append("namespace ");
            code.Append(assemblyName);
            code.AppendLine();
            code.AppendLine("{");
            code.Append($"   public class {typeName}AutoGenerated");

            code.AppendLine();
            code.AppendLine("   {");
            code.AppendLine();

            for (int i = 0; i < propertyCount; i++)
            {
                ItemType itemType       = (ItemType)reader.ReadInt16(); // ItemType Enum
                uint     propertyTypeId = reader.ReadUInt32();          // TypeId (Type Hash)
                string   propertyName   = reader.ReadString();          // Property Name
                bool     isNullable     = reader.ReadBool();            // Nullable Flag

                // add property code
                Type propType = ValueItem.GetRuntimeType(itemType);

                if (propType == null)
                {
                    propType = typeof(object);
                }

                code.AppendLine($"      public {TypeService.GetSourceCodeTypeName(propType)} {propertyName} {{get; set;}}");
                code.AppendLine();
            }

            code.AppendLine();
            code.AppendLine("   }");
            code.AppendLine("}");

            var type = TypeService.ImplementDynamicType(code.ToString(), assemblyName);

            ComplexStructure autoCreateStructure = new ComplexStructure(type, null, null, null, ctx);

            autoCreateStructure.TypeId = expectedTypeId;    // reset type id because of object only properties

            // Register auto created type
            BinarySerializer.RegisterTolerantTypeMapping(autoCreateStructure);

            return(autoCreateStructure);
        }