public virtual void OnField(string name, ushort id, object obj, bool withHeader = true) { if (withHeader) { OnFieldBegin(name, id, SirenMachine.GetTypeId(obj.GetType())); OnValue(obj); OnFieldEnd(); } else { OnValue(obj); } }
public virtual T OnField <T>(string name, ushort id, bool withHeader = true) { if (withHeader) { ushort outId; SirenTypeId outDataType; int r = OnFieldBegin(name, id, SirenMachine.GetTypeId(typeof(T)), out outId, out outDataType); if (r == 0) { var obj = OnValue(typeof(T)); OnFieldEnd(); return((T)obj); } return(default(T)); } else { return((T)OnValue(typeof(T))); } }
public SirenField(SirenCustomClass parentSirenClass, PropertyInfo info, SirenFieldAttribute attribute, uint index, ushort id) { ParentType = parentSirenClass; Info = info; Name = Info.Name; Id = id; Attribute = attribute; Index = index; FieldType = SirenPropertyFieldType.Struct; Type = SirenMachine.GetType(info.PropertyType); DataType = SirenMachine.GetTypeId(Type.Type); if (Type.Type.IsGenericType) { if (Type.Name.StartsWith("List")) { FieldType = SirenPropertyFieldType.List; var valueType = Type.Type.GetGenericArguments()[0]; ValueType = SirenMachine.GetType(valueType); } else if (Type.Name.StartsWith("Dictionary")) { FieldType = SirenPropertyFieldType.Dictionary; var keyType = Type.Type.GetGenericArguments()[0]; var valueType = Type.Type.GetGenericArguments()[1]; KeyType = SirenMachine.GetType(keyType); ValueType = SirenMachine.GetType(valueType); } else { throw new Exception("Invalid siren field"); } DefaultValueString = String.Empty; } else { if (Type.Type.IsPrimitive || Type.Type == typeof(string) || Type.Type == typeof(byte[])) { if (Type.Type == typeof(string)) { FieldType = SirenPropertyFieldType.String; } else if (Type.Type == typeof(byte[])) { FieldType = SirenPropertyFieldType.Blob; } else { FieldType = SirenPropertyFieldType.Value; } } else { FieldType = SirenPropertyFieldType.Struct; ValueType = SirenMachine.GetType(Type.Type); } DefaultValueString = String.Empty; if (Type.Type.IsValueType) { if (Attribute.DefaultValue != null) { if (Type.Type.IsEnum) { DefaultValueString = String.Format("({0}){1}", Type.Name, Convert.ToUInt32(Attribute.DefaultValue)); } else { DefaultValueString = Attribute.DefaultValue.ToString(); } } //else //{ // if (Type.Type == typeof(bool)) // { // DefaultValueString = "false"; // } // else if (Type.Type == typeof(float)) // { // DefaultValueString = "0.f"; // } // else if (Type.Type == typeof(double)) // { // DefaultValueString = "0.0"; // } // else // { // if (Type.Type.IsEnum) // { // DefaultValueString = String.Format("({0})0", Type.Name); // } // else // { // DefaultValueString = "0"; // } // } //} } } }