I found a post with a great answer for a problem I have, but I can't seem to find a small detail I'm looking for.
public class myModel
{
[JsonProperty(PropertyName = "id")]
public long ID { get; set; }
[JsonProperty(PropertyName = "some_string")]
public string SomeString {get; set;}
}
I need a method which returns the JsonProperty
PropertyName
of a specific property. Maybe something where I can pass the Type
and the Property
I need, and the method returns the value if found.
Here's the method I found which has me in the right direction (I believe) taken from here
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
...
public static string GetFields(Type modelType)
{
return string.Join(",",
modelType.GetProperties()
.Select(p => p.GetCustomAttribute<JsonPropertyAttribute>()
.Where(jp => jp != null)
.Select(jp => jp.PropertyName));
}
The goal is to call a function like this (any modification is fine)
string field = GetField(myModel, myModel.ID);
Update #1
I modified the above to this, but I don't know how to get the string of ID
from myModel.ID
.
public static string GetFields(Type modelType, string field) {
return string.Join(",",
modelType.GetProperties()
.Where(p => p.Name == field)
.Select(p => p.GetCustomAttribute<JsonPropertyAttribute>())
.Where(jp => jp != null)
.Select(jp => jp.PropertyName)
);
}
I want to prevent hard-coding strings of actual property names. For example I do not want to call the above method as:
string field = GetField(myModel, "ID");
I would rather use something like
string field = GetField(myModel, myModel.ID.PropertyName);
But I'm not entirely sure how to properly do this.
Thanks!