3

Ciao,

I'm frustrated about this problem, I have created a simple class like following:

public class Classe
{
    public int Intero { get; set; }

    public Int32 Intero32 { get; set; }

    public double Double { get; set; }

    public string Stringa { get; set; }

    public Classe PerReferenza { get; set; }
}

And I've written this extension method with the goal of return default value of a property (referenced type or value type):

public static class TypeExtensions
{
    public static object GetDefaultValue(this Type t)
    {
        if (t.IsValueType)
            return Activator.CreateInstance(t);

        return null;
    }
}

Following my Main method:

static void Main(string[] args)
{
    Classe c = new Classe();

    foreach (var proprietà in c.GetType().GetProperties())
    {
        var predefinito = proprietà.GetType().GetDefaultValue();

        Console.WriteLine($"Default for {proprietà}: {predefinito ?? "NULL"}");
    }

    Console.ReadKey();
}

This is my output:

Default for Int32 Intero: NULL
Default for Int32 Intero32: NULL
Default for Double Double: NULL
Default for System.String Stringa: NULL
Default for ConsoleApp1.Classe PerReferenza: NULL

I can't understand why I obtain alway FALSE for all properties... The expected output is:

Default for Int32 Intero: 0
Default for Int32 Intero32: 0
Default for Double Double: 0
Default for System.String Stringa: ""
Default for ConsoleApp1.Classe PerReferenza: null

Thank you a lot...

ilMattion
  • 1,841
  • 2
  • 24
  • 47

1 Answers1

11

You mean proprietà.PropertyType.GetDefaultValue(); you're currently asking whether RuntimePropertyInfo is a value-type (which: it isn't).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Hi Marc, thank you for your answer, How can I obtain, at runtime, the default of a property? – ilMattion Feb 04 '19 at 15:47
  • 1
    @ilMattion, With the change Marc suggests, your approach should be correct. – Olivier Jacot-Descombes Feb 04 '19 at 15:49
  • @ilMattion that depends on what you mean by "default". For example, you could look for a `DefaultValueAttribute` on the property, but neither the presence nor absence of that metadata is conclusive. If you mean "the default of the type of the property" - you pretty much have that already – Marc Gravell Feb 04 '19 at 15:49
  • 1
    @ilMattion: `proprietà.PropertyType` is the type of the property. `proprietà.GetType()` is the type of the property information, i.e. the type of the meta-data returned by Reflection. You want the first one. – Olivier Jacot-Descombes Feb 04 '19 at 15:53
  • Thank you a lot at all! It is a stupid question but I've lost a lot of time...thanks – ilMattion Feb 04 '19 at 15:55
  • @iMattion: You will find that, when you hit problems like this, if you split statements into intermediate variables and then debug things carefully, you can often find your problem. For example, if you split `var predefinito = proprietà.GetType().GetDefaultValue();` into `var myType = proprietà.GetType();` and then `var predefinito = myType.GetDefaultValue();` the debugger will reveal that `myType` isn't what you were expecting. You can rely on the optimizer to eliminate those intermediate variables in release builds. – Flydog57 Feb 04 '19 at 16:29