-3

The expected result is to get the method parameter T property Name. Here is my code,

I have tried few suggested workarounds to make use of class ABC typeof(ABC).GetProperties - didn't get the expected result.

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}";
}

Expecting result in this line

var resultName = nameof(field); // should return as "Name" 
Arjun
  • 53
  • 1
  • 3
  • Since `nameof (field) == "field"` and `ToString` returns the default object type or what is defined in overriding methods in the hierarchy, what is the problem and question? What is your goal and expected result? In case it helps: [Get parameter's "source caller's variable name"](https://stackoverflow.com/questions/65754778/how-to-check-if-the-parameter-of-a-method-comes-from-a-variable-or-a-literal/65754971#65754971) –  Aug 20 '21 at 13:45
  • You are using the Property value and not the Property itself by using item.Name. You need to use Reflection. https://learn.microsoft.com/fr-fr/dotnet/api/system.type.getproperties?view=net-5.0 – KiwiJaune Aug 20 '21 at 13:48
  • This is not easy to do without reflection (it would involve passing expression trees) and not efficient in any scenario. Your current `AddTest` method is trivial and should not exist (just write out the interpolated strings, using `nameof` in the `ExecuteMain` method); for more general scenarios you could have something more complicated, but only if it can really be shown to be necessary. – Jeroen Mostert Aug 20 '21 at 13:48
  • What result do you expect when I call `AddTest("Hello world")`? It's a literal, not a property, so it has no name. Or how about `AddTest(item.Name + "")`? – John Wu Aug 20 '21 at 14:35

1 Answers1

1

I don't know why you are trying to do that, but this is how you should write it :

    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}";
    }

However, Reflection is slow to run.

If it's just to have the name, you could do much simpler :

    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}";
    }
KiwiJaune
  • 530
  • 2
  • 16