1

How can I achieve what I'm trying to do here:

public class MyClassName
{
    [JsonProperty(nameof(MyProperty).ToJsonIdentifier())]
    public bool MyProperty{ get; set; }
}

I have an extension method that can convert the string name of the property to the format I need. But this is not valid c# syntax. Is there a overload or ability to extend the JsonProperty decorator to allow me to specify a function to calculate the desired json property name.

Update: I have a suspicion that perhaps a lambda function could be passed in?

Update2: Is it possible that this would work?

public class MyClassName
{
    [SubClassOfJsonProperty(() => nameof(MyProperty).ToJsonIdentifier())]
    public bool MyProperty{ get; set; }
}
user230910
  • 2,353
  • 2
  • 28
  • 50
  • You need to use customresolver. Look here: https://stackoverflow.com/questions/48348469/change-json-property-name-in-output-using-json-net – MistyK Jun 27 '18 at 07:22
  • doesnt quite match, I only want it for a specific property or 6 on this class, not as a general thing.. – user230910 Jun 27 '18 at 07:30
  • to address the duplicate tag - i'm looking for a more general solution - i need to execute a function to send info to a decorator parameter – user230910 Jun 27 '18 at 07:33
  • 1
    Attributes are compile-time things, you can't change them at runtime. Choose a different approach, see duplicate. – CodeCaster Jun 27 '18 at 07:33
  • movenext is interesting, but unrelated i guess – user230910 Jun 27 '18 at 07:36
  • @codecaster, "you cannot do that" is a valid answer, but i'm pretty sure ive seen lambdas passed in before? – user230910 Jun 27 '18 at 07:37
  • If your question is _"How to change a JSON property name at runtime"_, which it's currently almost literally, your question is answered by the duplicate. If your question is _"How can I determine an attribute value at runtime"_, the answer is _"You can't"_, of which I'm sure there is another duplicate or two. If you're asking _"Can I use a lambda as an attribute parameter"_, then that too is a duplicate, see [Lambda expression in attribute constructor](https://stackoverflow.com/questions/16809294/). All those questions have been asked before, try searching. – CodeCaster Jun 27 '18 at 07:40
  • is it possible that something like my lambda thing could work? at least for other decorators? – user230910 Jun 27 '18 at 07:41
  • @codecaster fair enough - i think my question is the second, sorry for the bad formatting - – user230910 Jun 27 '18 at 07:42

0 Answers0