Ejemplo n.º 1
0
        public static object AutoSetEntity(object entity)
        {
            if (entity.GetType().GetProperty("Id") != null && entity.GetType().GetProperty("Id").GetValue(entity, null).AsLong() == 0)
            {
                var id = Snowflake.GenerateId();
                entity.GetType().GetProperty("Id").SetValue(entity, id, null);
            }
            var properties = entity.GetType().GetProperties();

            foreach (var property in properties)
            {
                if (!property.CanWrite)
                {
                    continue;
                }
                if (property.PropertyType == typeof(string) && property.GetValue(entity, null) == null)
                {
                    property.SetValue(entity, "", null);
                }
                else if (property.PropertyType == typeof(int?) && property.GetValue(entity, null) == null)
                {
                    property.SetValue(entity, 0, null);
                }
                else if (property.PropertyType == typeof(decimal?) && property.GetValue(entity, null) == null)
                {
                    property.SetValue(entity, 0.00M, null);
                }
            }
            return(entity);
        }