What is the best way to add a static method to an existing C # class?

0

I'm creating a restfull application that uses the Newtonsoft library to send information through controllers . In my application, I have numerous classes like for example the user:

public class UsuarioModel
{
    public int Id { get; set; }
    public string Login { get; set; }
    public string Senha { get; set; }
    public string Email { get; set; }
    public bool Visivel { get; set; }
    public DateTime? CriadoEm { get; set; }
    public DateTime? AtualizadoEm { get; set; }
}

In order to not send null or default properties I want to override the static property method within the JObject.FromObject class like this:

public class JObject
{
    public static JObject FromObject(object o)
    {
        return FromObject(o, new JsonSerializer()
        {
            NullValueHandling = NullValueHandling.Ignore,
            DefaultValueHandling = DefaultValueHandling.Ignore
        });
    }
}

The intention to do this is that I can write less code and standardize the entire application to always use this setting when calling the method.

The problem is that (as far as I know) it can not do a override of a static method within an extension class .

What should I do to implement this method?

    
asked by anonymous 23.11.2018 / 14:54

2 answers

1

My solution was to create the method I wanted as an extension of the class and with another name, like this:

public static class Extensions
{
    public static JObject FromObjectCompressed(this JObject jObject, object o)
    {
        return JObject.FromObject(o, new JsonSerializer()
        {
            NullValueHandling = NullValueHandling.Ignore,
            DefaultValueHandling = DefaultValueHandling.Ignore
        });
    }
    public static JArray FromArrayCompressed(this JArray jObject, object o)
    {
        return JArray.FromObject(o, new JsonSerializer()
        {
            NullValueHandling = NullValueHandling.Ignore,
            DefaultValueHandling = DefaultValueHandling.Ignore
        });
    }
}
    
23.11.2018 / 18:39
1

If you have access to the class just create a static method in it. If you want to override do not want a static method, the two things are inconsistent. A static method exists globally, for the class and therefore for the application. A method that can be overwritten is virtual , so it adopts dynamic polymorphism. It only makes sense to use this kind of mechanism in instances of a class, therefore in the object.

If you do not have access just create another class with the method you want and call this method from your class instead of calling the original class. If you can not do this you need another solution, you are not creating a new static method that will achieve. Without a larger context it does not have much to help.

The question does not make it clear why it would want this, but it does not seem to be necessary and nothing goes wrong there.

In the future it looks like you might have static method extension in classes, but even if you have it, it's not something to be abused and much less will allow override because it will still make no sense.

Incidentally, this JSON component will be considered obsolete in the next version of .NET Core. Almost every time that uses object panda dies in China: (

    
23.11.2018 / 15:07