Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the JsonPropertyAttribute of a Property

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!

like image 202
RoLYroLLs Avatar asked Aug 22 '17 19:08

RoLYroLLs


1 Answers

Here's a way of doing it while keeping things strongly typed:

public static string GetPropertyAttribute<TType>(Expression<Func<TType, object>> property)
{
    var memberExpression = property.Body as MemberExpression;
    if(memberExpression == null)
        throw new ArgumentException("Expression must be a property");

    return memberExpression.Member
        .GetCustomAttribute<JsonPropertyAttribute>()
        .PropertyName;
}

And call it like this:

var result = GetPropertyAttribute<myModel>(t => t.SomeString);

You can make this a bit more generic, for example:

public static TAttribute GetPropertyAttribute<TType, TAttribute>(Expression<Func<TType, object>> property)
where TAttribute : Attribute
{
    var memberExpression = property.Body as MemberExpression;
    if(memberExpression == null)
        throw new ArgumentException("Expression must be a property");

    return memberExpression.Member
        .GetCustomAttribute<TAttribute>();
}

And now because the attribute is generic, you need to move the PropertyName call outside:

var attribute = GetPropertyAttribute<myModel, JsonPropertyAttribute>(t => t.SomeString);
var result = attribute.PropertyName;
like image 173
DavidG Avatar answered Sep 29 '22 03:09

DavidG