【问题标题】:How to get generic method parameter property name in C#? [duplicate]如何在 C# 中获取泛型方法参数属性名称? [复制]
【发布时间】:2021-08-20 13:39:46
【问题描述】:

预期的结果是获取方法参数T属性名称。这是我的代码,

我尝试了一些建议的解决方法来使用类 ABC typeof(ABC).GetProperties - 没有得到预期的结果。

public class ABC
{
  public string Name { get; set; }
  public int RecordCount { get; set; }
  public decimal Total { get; set; }
  public DateTime CreatedDate { get; set; }
}

public void ExecuteMain()
{
  var item = new ABC
  { 
    Name = "TestUser A", 
    RecordCount = 10, 
    Total = 100.20m, 
    CreatedDate = DateTime.Now 
  };
  AddTest<string>(item.Name);
  AddTest<int>(item.RecordCount);
  AddTest<decimal>(item.Total);
  AddTest<DateTime>(item.CreatedDate);
}

private string AddTest<T>(T field)
{
  var resultName = nameof(field); // should return as "Name" 
  var resultValue = field.ToString(); // this returns "TestUser A" which is correct

  //Record Count, Total, CreatedDate  to add later
  return $"Name = {resultName}:{resultValue}";
}

期待这一行的结果

var resultName = nameof(field); // should return as "Name" 

【问题讨论】:

  • 既然nameof (field) == "field"ToString 返回默认的对象类型或层次结构中覆盖方法中定义的内容,那么问题和疑问是什么?你的目标和预期结果是什么?如果有帮助:Get parameter's "source caller's variable name"
  • 您使用的是属性值,而不是使用 item.Name 的属性本身。您需要使用反射。 docs.microsoft.com/fr-fr/dotnet/api/…
  • 这在没有反射的情况下不容易做到(它会涉及传递表达式树)并且在任何情况下都没有效率。您当前的AddTest 方法很简单,不应该存在(只需写出插入的字符串,在nameof 方法中使用nameof);对于更一般的场景,你可以有更复杂的东西,但前提是它确实被证明是必要的。
  • 当我致电AddTest&lt;string&gt;("Hello world") 时,您希望得到什么结果?它是文字,而不是属性,因此没有名称。或者AddTest&lt;string&gt;(item.Name + "")怎么样?

标签: c# generics reflection


【解决方案1】:

我不知道你为什么要这么做,但你应该这样写:

    using System.Reflection;

    public class ABC
    {
        public string Name { get; set; }
        public int RecordCount { get; set; }
        public decimal Total { get; set; }
        public DateTime CreatedDate { get; set; }
    }

    public void ExecuteMain()
    {
        var item = new ABC
        {
            Name = "TestUser A",
            RecordCount = 10,
            Total = 100.20m,
            CreatedDate = DateTime.Now
        };
        AddTest(item.GetType().GetProperty(nameof(item.Name)), item);
        AddTest(item.GetType().GetProperty(nameof(item.RecordCount)), item);
        AddTest(item.GetType().GetProperty(nameof(item.Total)), item);
        AddTest(item.GetType().GetProperty(nameof(item.CreatedDate)), item);
    }

    private string AddTest(PropertyInfo prop, object o)
    {
        var resultName = prop.Name; // should return as "Name" 
        var resultValue = prop.GetValue(o); // this returns "TestUser A" which is correct

        //Record Count, Total, CreatedDate  to add later
        return $"Name = {resultName}:{resultValue}";
    }

但是,Reflection 运行缓慢。

如果只是有名字,你可以做得更简单:

    public void ExecuteMain()
    {
        var item = new ABC
        {
            Name = "TestUser A",
            RecordCount = 10,
            Total = 100.20m,
            CreatedDate = DateTime.Now
        };
        AddTest(nameof(item.Name), item.Name);
        AddTest(nameof(item.RecordCount), item.RecordCount);
        AddTest(nameof(item.Total), item.Total);
        AddTest(nameof(item.CreatedDate), item.CreatedDate);
    }

    private string AddTest(String resultName, object resultValue)
    {
        return $"Name = {resultName}:{resultValue}";
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 2016-02-03
    • 2019-05-16
    • 1970-01-01
    • 2014-07-23
    • 2020-08-28
    相关资源
    最近更新 更多