Programmatic equivalent of default(Type)

前端 未结 14 1836
时光取名叫无心
时光取名叫无心 2020-11-22 05:28

I\'m using reflection to loop through a Type\'s properties and set certain types to their default. Now, I could do a switch on the type and set the defau

14条回答
  •  借酒劲吻你
    2020-11-22 06:22

    The chosen answer is a good answer, but be careful with the object returned.

    string test = null;
    string test2 = "";
    if (test is string)
         Console.WriteLine("This will never be hit.");
    if (test2 is string)
         Console.WriteLine("Always hit.");
    

    Extrapolating...

    string test = GetDefault(typeof(string));
    if (test is string)
         Console.WriteLine("This will never be hit.");
    

提交回复
热议问题