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);
Thanks!
Aucun commentaire:
Enregistrer un commentaire