Esempio n. 1
0
        /// <summary>
        /// 实体的属性描述
        /// </summary>
        /// <param name="property">属性信息</param>
        public EntityPropertyDescriptor(PropertyInfo property)
            : base(property)
        {
            var typeAttr = property.GetCustomAttribute <ColumnTypeAttribute>();

            if (typeAttr != null)
            {
                this.ColumnType = typeAttr.ColumnType;
            }

            var nameAttr = property.GetCustomAttribute <ColumnNameAttribute>();

            if (nameAttr != null)
            {
                this.Name = LineProtocolUtil.Encode(nameAttr.Name);
            }
            this.Utf8Name = Encoding.UTF8.GetBytes(this.Name);

            var type = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;

            if (this.ColumnType == ColumnType.Timestamp)
            {
                this.valueConverter = CreateTimestampConverter(type);
            }
            else if (this.ColumnType == ColumnType.Field)
            {
                this.valueConverter = LineProtocolUtil.CreateFieldValueConverter(type);
            }
            else // 标签
            {
                this.valueConverter = value => LineProtocolUtil.Encode(value?.ToString());
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 获取时间戳转换器
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private Func <object?, string?> CreateTimestampConverter(Type type)
        {
            if (type == typeof(DateTime))
            {
                return(value => LineProtocolUtil.GetNsTimestamp((DateTime?)value).ToString());
            }

            if (type == typeof(DateTimeOffset))
            {
                return(value => LineProtocolUtil.GetNsTimestamp((DateTimeOffset?)value)?.ToString());
            }

            throw new NotSupportedException($"属性{type} {this.Name}不支持转换为Timestamp");
        }