c# - 在.net core 3.1中获取InlfuxDB测量属性的属性值
问题描述
本周我开始在我的一个 .net core 3.1 应用程序中使用 InfluxDB.Client.Core 包进行开发。按照教程和阅读包中的文档,一切正常:https ://github.com/influxdata/influxdb-client-csharp 。
问题是我无法从流入测量属性中获取值。
模型:
[Measurement("temperature")]
private class TemperatureDataModel
{
[Column("location", IsTag = true)] public string Location { get; set; }
[Column("value")] public double Value { get; set; }
[Column(IsTimestamp = true)] public DateTime Time;
}
按照https://docs.microsoft.com/en-us/dotnet/standard/attributes/retrieving-information-stored-in-attributes上的 microsoft 文档,我写了这个
方法:
internal static string GetMeasurementAttributeName(Type t)
{
var testingNameOfT = t.Name;
var MyAttribute = (Measurement)Attribute.GetCustomAttribute(t, typeof(Measurement));
return MyAttribute?.Name;
}
- testingNameOfT返回(如预期)TemperatureDataModel。
- MyAttribute返回 null
那么我怎么可能无法获得我的 TemperatureDataModel 的属性。
InfluxDB.Client.Core 包的代码是:
using System;
namespace InfluxDB.Client.Core
{
public sealed class Measurement : Attribute
{
public string Name { get; }
public Measurement(string name)
{
this.Name = name;
}
}
}
我们通过将模型重写为:
private class TemperatureDataModel
{
[Column("location", IsTag = true)] public string Location { get; set; }
[Column("value")] public double Value { get; set; }
[Column(IsTimestamp = true)] public DateTime Time;
[Measurement] public string Measurement{ get; set; } //=> will use the value of the property instead of [Measurement("temperature")]
}
长话短说:我怎么可能无法使用 Microsoft 的示例代码读取属性?: var attribute = (Measurement)Attribute.GetCustomAttribute(t, typeof(Measurement)) //=> null :(
解决方案
推荐阅读
- azure - 为 Azure SQL 数据库创建发布配置文件
- python - 如何使用python在字符串中的单词后查找值?
- java - RestTemplate 和 UriComponentsBuilder 访问 API
- sql-server - 检查存储过程中的权限
- facebook-graph-api - 我可以在我的后端访问有关 Instagram 媒体的 Instagram 见解吗?
- asp.net - FluentValidation 验证字段总和
- javascript - HTML获取React中表单的内部html标签值onSubmit
- angular - 在循环中获取 ngModel 类型元素的值
- python - 如何更改类中的变量并返回它
- c++ - 在 C++11 中返回包含 const-reference 的 std::tuple