Esempio n. 1
0
            public EntityFieldsMapper(Type entityType, PersistentStorage ps)
            {
                _ps = ps;
                ObjectDescription od = ClassFactory.GetObjectDescription(entityType, ps);

                foreach (PropertyDescription prop in od.Properties)
                {
                    string             dbPropName = "";
                    FieldNameAttribute attr       = prop.GetAttribute <FieldNameAttribute>();
                    if (attr == null)
                    {
                        dbPropName = prop.Name;
                    }
                    else if (prop.ReflectedObject.GetAttribute <IdFieldNameAttribute>() != null &&
                             prop.ReflectedObject.GetAttribute <IdFieldNameAttribute>().Name == prop.Name)
                    {
                        dbPropName = prop.ReflectedObject.GetAttribute <IdFieldNameAttribute>().Name;
                    }
                    else
                    {
                        dbPropName = attr.FieldName;
                    }
                    _entityFieldsMapping.Add(prop.Name, dbPropName);
                    _entityFieldsMappingInverse.Add(dbPropName, prop.Name);
                }
            }
        /// <summary>
        /// Reads annotation fields for event name and field-values and set them to
        /// proper event fields. After this method, json serialization of event will meet server expectation
        /// </summary>
        /// <param name="gameEvent">always is caller event. Just for test purposes</param>
        private void InitFieldsUsingAnnotations(BacktoryGameEvent gameEvent)
        {
            var temp            = new List <FieldValue>();
            var fieldNamesProps = Utils.GetPropertiesByAttribute(typeof(FieldNameAttribute), this, true);

            foreach (var prop in fieldNamesProps)
            {
                FieldNameAttribute attr = prop.GetCustomAttributes(typeof(FieldNameAttribute), true).Single() as FieldNameAttribute;
                if (attr == null)
                {
                    throw new InvalidOperationException("something bad in reflection");
                }

                var val = prop.GetValue(this, null);
                if (val is int)
                {
                    temp.Add(new FieldValue(attr.Name, (int)val));
                }
                else
                {
                    throw new ArgumentException("\"Fields\"s can only accept integers as their values. Your's is " + val.GetType());
                }
            }
            FieldsAndValues = temp;
        }