首页 > 解决方案 > 在.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;
}

那么我怎么可能无法获得我的 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 :(

标签: c#.net-coreattributesinfluxdbinfluxdb-2

解决方案


推荐阅读