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

Asked

Viewed 94 times

-1

I’m creating an app restfull using the library Newtonsoft to send information through controllers. In my application, I have numerous classes such as 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; }
}

So that no properties are sent null or default i want to overwrite the property static method within the class JObject.FromObject thus:

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

The intention of doing this is that I can write less code and standardize the entire application forever use this setting when calling the method.

The problem is that (as far as I know) there is no way to make a override of a static method within a Extension class.

What should I do to implement this method?

2 answers

1

If you have access to the class just create a static method in it. If you want to make one override does 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 allows it to be overwritten is virtual, therefore adopts dynamic polymorphism. It only makes sense to use this type of mechanism in instances of a class, therefore in the object.

If you don’t 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’t do this, you need another solution, not by creating a new static method that you’re going to get. Without a bigger context there’s not much to help.

The question does not make it clear why I would want this, but it seems that it is not necessary and nothing wrong occurs there.

In the future it seems that it may have extension of static method in classes, but even if it has, it is not something to be abused and much less will allow override because it still doesn’t make sense.

In fact, this JSON component will be considered obsolete in the next version of . NET Core. Almost every time you use object a panda dies in China :(

  • I understand what you say, the question itself is the curiosity I had if in C# it was possible to do something of the kind or better.

1


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

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
        });
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.