public object Clone() { SirenField val = (SirenField)MemberwiseClone(); val.Attribute = Attribute.Clone() as SirenFieldAttribute; return(val); }
public override bool LoadFrom(Stream stream) { base.LoadFrom(stream); Attribute.LoadFrom(stream); BaseTypeName = stream.ReadString(); //types uint typeCount = stream.ReadUInt(); for (int i = 0; i < typeCount; i++) { byte isClass = (byte)stream.ReadByte(); if (isClass == 1) { SirenCustomClass type = new SirenCustomClass(); type.LoadFrom(stream); type.Parent = this; Types.Add(type.Name, type); } else { SirenCustomEnum type = new SirenCustomEnum(); type.LoadFrom(stream); type.Parent = this; Types.Add(type.Name, type); } } //fields uint fieldCount = stream.ReadUInt(); for (int i = 0; i < fieldCount; i++) { SirenField field = new SirenField(); field.LoadFrom(stream); field.ParentType = this; FieldNameDict.Add(field.Name, field); field.Index = (ushort)(FieldNameDict.Count - 1); } return(true); }
public bool Initialize() { if (Type.IsEnum) { return(true); } //get methods SerializeMethodInfo = Type.GetMethod("Serialize", BindingFlags.Public | BindingFlags.Instance); DeserializeMethodInfo = Type.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Instance); RegisterMethodInfo = Type.GetMethod("Register", BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); if (RegisterMethodInfo != null && !Type.ContainsGenericParameters) { RegisterMethodInfo.Invoke(null, null); } //get properties uint index = 0; ushort id = GetBasePropertyCount(); var properties = Type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance); foreach (var propertyInfo in properties) { var attrs = propertyInfo.GetCustomAttributes(typeof(SirenFieldAttribute), false); if (attrs.Length > 0) { SirenFieldAttribute propertyAttribute = attrs[0] as SirenFieldAttribute; if (propertyAttribute != null) { SirenField property = new SirenField(this, propertyInfo, propertyAttribute, index++, id++); Fields.Add(property); FieldIdDict.Add(property.Id, property); FieldNameDict.Add(property.Name, property); } } } if (Fields.Count == 0) { if (Type.BaseType == typeof(object) || Type.BaseType.IsValueType) { return(false); } } if (!Type.IsValueType && Type.BaseType != typeof(object) && !Type.BaseType.IsValueType) { BaseType = SirenMachine.GetClass(Type.BaseType); if (BaseType != null) { //add base properties foreach (var sirenProperty in BaseType.FieldIdDict) { FieldIdDict.Add(sirenProperty.Key, sirenProperty.Value); } foreach (var sirenProperty in BaseType.FieldNameDict) { FieldNameDict.Add(sirenProperty.Key, sirenProperty.Value); } } } return(true); }
public override bool LoadFrom(Stream stream) { base.LoadFrom(stream); Attribute.LoadFrom(stream); BaseTypeName = stream.ReadString(); //types uint typeCount = stream.ReadUInt(); for (int i = 0; i < typeCount; i++) { byte isClass = (byte)stream.ReadByte(); if (isClass == 1) { SirenCustomClass type = new SirenCustomClass(); type.LoadFrom(stream); type.Parent = this; Types.Add(type.Name, type); } else { SirenCustomEnum type = new SirenCustomEnum(); type.LoadFrom(stream); type.Parent = this; Types.Add(type.Name, type); } } //fields uint fieldCount = stream.ReadUInt(); for (int i = 0; i < fieldCount; i++) { SirenField field = new SirenField(); field.LoadFrom(stream); field.ParentType = this; FieldNameDict.Add(field.Name, field); field.Index = (ushort)(FieldNameDict.Count - 1); } return true; }
public bool Initialize() { if (Type.IsEnum) { return true; } //get methods SerializeMethodInfo = Type.GetMethod("Serialize", BindingFlags.Public | BindingFlags.Instance); DeserializeMethodInfo = Type.GetMethod("Deserialize", BindingFlags.Public | BindingFlags.Instance); RegisterMethodInfo = Type.GetMethod("Register", BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); if (RegisterMethodInfo != null && !Type.ContainsGenericParameters) { RegisterMethodInfo.Invoke(null, null); } //get properties uint index = 0; ushort id = GetBasePropertyCount(); var properties = Type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance); foreach (var propertyInfo in properties) { var attrs = propertyInfo.GetCustomAttributes(typeof(SirenFieldAttribute), false); if (attrs.Length > 0) { SirenFieldAttribute propertyAttribute = attrs[0] as SirenFieldAttribute; if (propertyAttribute != null) { SirenField property = new SirenField(this, propertyInfo, propertyAttribute, index++, id++); Fields.Add(property); FieldIdDict.Add(property.Id, property); FieldNameDict.Add(property.Name, property); } } } if (Fields.Count == 0) { if (Type.BaseType == typeof(object) || Type.BaseType.IsValueType) { return false; } } if (!Type.IsValueType&& Type.BaseType != typeof(object) && !Type.BaseType.IsValueType) { BaseType = SirenMachine.GetClass(Type.BaseType); if (BaseType != null) { //add base properties foreach (var sirenProperty in BaseType.FieldIdDict) { FieldIdDict.Add(sirenProperty.Key, sirenProperty.Value); } foreach (var sirenProperty in BaseType.FieldNameDict) { FieldNameDict.Add(sirenProperty.Key, sirenProperty.Value); } } } return true; }