Ejemplo n.º 1
0
        public PropertyData(AssemblyData g, MemberInfo member)
        {
            this.Member = member;
            this.Name   = member.Name;

            if (HasNotBinarySerializableAttribute(member))
            {
                return;
            }

            var forceSerialize = HasBinarySerializableAttribute(member);

            var field = member as FieldInfo;

            if (field != null)
            {
                if (forceSerialize || (field.IsPublic && CanSerialize(field.FieldType)))
                {
                    this.Type           = g.GetPropertyType(field.FieldType);
                    this.IsSerializable = this.Type.Read != null && this.Type.Write != null;
                }
            }

            var property = member as PropertyInfo;

            if (property != null)
            {
                if (property.GetIndexParameters().Length > 0 || !CanSerialize(property.PropertyType))
                {
                    return;
                }

                var get = property.GetGetMethod(true);
                if (get == null)
                {
                    return;
                }

                if (forceSerialize || get.IsPublic)
                {
                    this.Type           = g.GetPropertyType(property.PropertyType);
                    this.IsSerializable = this.Type.Read != null && this.Type.Write != null;
                    if (this.IsSerializable)
                    {
                        var set = property.GetSetMethod(true);
                        if (set == null)
                        {
                            this.IsSerializable = this.Type.Read.ReadIntoExistingInstance;
                        }
                        else if (!set.IsPublic)
                        {
                            this.IsSerializable = this.Type.Read.ReadIntoExistingInstance || forceSerialize;
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public PropertyTypeData GetPropertyType(Type type)
        {
            PropertyTypeData result;

            if (!PropertyTypes.TryGetValue(type, out result))
            {
                PropertyTypes[type] = result = new PropertyTypeData(this, type);
            }
            return(result);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Create the template output
 /// </summary>
 public virtual string TransformText()
 {
     foreach (var namespaceGroup in g.Types.GroupBy(x => x.Namespace).OrderBy(x => x.Key))
     {
         this.Write("namespace ");
         this.Write(this.ToStringHelper.ToStringWithCulture(namespaceGroup.Key));
         this.Write("\r\n{\r\n");
         foreach (var type in namespaceGroup.OrderBy(x => x.Name))
         {
             this.Write("    [global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"");
             this.Write(this.ToStringHelper.ToStringWithCulture(g.GeneratorName));
             this.Write("\", \"");
             this.Write(this.ToStringHelper.ToStringWithCulture(g.GeneratorVersion));
             this.Write("\")]\r\n    class ");
             this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
             this.Write("Writer : Nine.Serialization.IBinaryObjectWriter\r\n    {\r\n        public System.Typ" +
                        "e ReaderType { get { return typeof(");
             this.Write(this.ToStringHelper.ToStringWithCulture(type.Namespace));
             this.Write(".");
             this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
             this.Write("Reader); } }\r\n        public System.Type TargetType { get { return typeof(");
             this.Write(this.ToStringHelper.ToStringWithCulture(PropertyTypeData.GetClassFullName(type.Type)));
             this.Write("); } }\r\n                \r\n        public void Write(System.IO.BinaryWriter output" +
                        ", object obj, System.IServiceProvider serviceProvider)\r\n        {\r\n            v" +
                        "ar value = (");
             this.Write(this.ToStringHelper.ToStringWithCulture(PropertyTypeData.GetClassFullName(type.Type)));
             this.Write(")obj;\r\n");
             foreach (var property in type.Properties)
             {
                 WriteLine(property.Type.ToWriteString("output", "value." + property.Name, 0));
             }
             this.Write("        }\r\n    }\r\n");
         }
         this.Write("}\r\n");
     }
     return(this.GenerationEnvironment.ToString());
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Create the template output
 /// </summary>
 public virtual string TransformText()
 {
     foreach (var namespaceGroup in g.Types.GroupBy(x => x.Namespace).OrderBy(x => x.Key))
     {
         this.Write("namespace ");
         this.Write(this.ToStringHelper.ToStringWithCulture(namespaceGroup.Key));
         this.Write("\r\n{\r\n");
         foreach (var type in namespaceGroup.OrderBy(x => x.Name))
         {
             this.Write("    [global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"");
             this.Write(this.ToStringHelper.ToStringWithCulture(g.GeneratorName));
             this.Write("\", \"");
             this.Write(this.ToStringHelper.ToStringWithCulture(g.GeneratorVersion));
             this.Write("\")]\r\n    class ");
             this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));
             this.Write("Reader : Nine.Serialization.IBinaryObjectReader\r\n    {\r\n        public object Rea" +
                        "d(System.IO.BinaryReader input, object existingInstance, System.IServiceProvider" +
                        " serviceProvider)\r\n        {\r\n            var result = existingInstance as ");
             this.Write(this.ToStringHelper.ToStringWithCulture(PropertyTypeData.GetClassFullName(type.Type)));
             this.Write(";\r\n            if (result == null)\r\n            {\r\n");
             if (type.ConstructorParameters != null)
             {
                 this.Write("                result = new ");
                 this.Write(this.ToStringHelper.ToStringWithCulture(PropertyTypeData.GetClassFullName(type.Type)));
                 this.Write("(\r\n");
                 for (var i = 0; i < type.ConstructorParameters.Length; i++)
                 {
                     if (i != 0)
                     {
                         this.Write(",");
                     }
                     this.Write(" ");
                     this.Write(this.ToStringHelper.ToStringWithCulture(PropertyTypeData.GetClassFullName(type.ConstructorParameters[i].DeclaringType)));
                     this.Write(".");
                     this.Write(this.ToStringHelper.ToStringWithCulture(type.ConstructorParameters[i].Name));
                     this.Write("(serviceProvider)\r\n");
                 }
                 this.Write(");\r\n");
             }
             else if (type.HasServiceProviderConstructor)
             {
                 this.Write("                result = new ");
                 this.Write(this.ToStringHelper.ToStringWithCulture(PropertyTypeData.GetClassFullName(type.Type)));
                 this.Write("(serviceProvider);\r\n");
             }
             else
             {
                 this.Write("                result = new ");
                 this.Write(this.ToStringHelper.ToStringWithCulture(PropertyTypeData.GetClassFullName(type.Type)));
                 this.Write("();\r\n");
             }
             this.Write("            }\r\n");
             if (type.SupportsInitialize)
             {
                 this.Write("            ((System.ComponentModel.ISupportInitialize)result).BeginInit();\r\n");
             }
             foreach (var property in type.Properties)
             {
                 WriteLine(property.Type.ToReadString("input", "result." + property.Name, 0));
             }
             if (type.SupportsInitialize)
             {
                 this.Write("            ((System.ComponentModel.ISupportInitialize)result).EndInit();\r\n");
             }
             this.Write("            return result;\r\n        }\r\n    }\r\n");
         }
         this.Write("}\r\n");
     }
     return(this.GenerationEnvironment.ToString());
 }