lundi 23 septembre 2019

C# Create default value for class, e.g. through reflection

Let's say I have a class A:

public class A 
{
    private int value;
    public A() => value = 0;
    public A(int value) => this.value = value;
}

And I have some method, with a parameter list where some are defaulted:

public void SomeMethod(int i, float f, string s = "", A a = null)

Now is there some way, e.g. through reflection, to be more smart about this parameter list? I would like to be able to do something like the following, so I don't need to check for null everywhere:

public void SomeMethod(int i, float f, string s = "", A a = Default) // should use the empty constructor!

Is this possible with C#?





Aucun commentaire:

Enregistrer un commentaire