Programmatic Equivalent of Default(Type)

Programmatic equivalent of default(Type)

  • In case of a value type use Activator.CreateInstance and it should work fine.
  • When using reference type just return null
public static object GetDefault(Type type)
{
if(type.IsValueType)
{
return Activator.CreateInstance(type);
}
return null;
}

In the newer version of .net such as .net standard, type.IsValueType needs to be written as type.GetTypeInfo().IsValueType

how to get the default value of a type if the type is only known as System.Type?

Since you really only have to worry about value types (reference types will just be null), you can use Activator.CreateInstance to call the default constructor on them.

public static object GetDefaultValue(Type type) {
return type.IsValueType ? Activator.CreateInstance(type) : null;
}

Edit: Jon is (of course) correct. IsClass isn't exhaustive enough - it returns False if type is an interface.

Default value of a type at Runtime

There's really only two possibilities: null for reference types and new myType() for value types (which corresponds to 0 for int, float, etc) So you really only need to account for two cases:

object GetDefaultValue(Type t)
{
if (t.IsValueType)
return Activator.CreateInstance(t);

return null;
}

(Because value types always have a default constructor, that call to Activator.CreateInstance will never fail).

Programmatic equivalent to setting default style in generic.xaml

After pondering deeply about this problem and recapitulating my (beginner's) knowledge of dependency properties, I found the simple solution, which I have proven to work. I just had to replace

Application.Current.Resources.Add(typeof(Trailer), defaultStyle);

by

FrameworkElement.StyleProperty.OverrideMetadata(typeof(Trailer), 
new FrameworkPropertyMetadata(defaultStyle));

which is nothing but the official way to change the default value of a DependencyProperty!

I have already thought into this direction, but my mistake was that I expected to find a "DefaultStyleProperty" (in correspondence to the DefaultStylePropertyKey...), but I didn't think about the easier and more natural possibility of changing the default value of the StyleProperty for my class.

I am glad that Microsoft's design once again turned out to be very logical and carefully crafted (Amen...).

Addendum: I have checked the visual trees with WPF inspector. They are identical irrespective of whether I supply a generic.xaml, or use the code-only method above.

As an additional feature of consistency I have added

FrameworkElement.OverridesDefaultStyleProperty.OverrideMetadata(typeof(Trailer), 
new FrameworkPropertyMetadata(true));

which keeps the default style from being loaded out of generic.xaml if it should ever be there at all. Since I am setting the style for myself anyway, this doesn't actually make a difference. Be careful though, I am not sure if this can cause unwanted side-effects.

Programmatic equivalent of default(Type)

  • In case of a value type use Activator.CreateInstance and it should work fine.
  • When using reference type just return null
public static object GetDefault(Type type)
{
if(type.IsValueType)
{
return Activator.CreateInstance(type);
}
return null;
}

In the newer version of .net such as .net standard, type.IsValueType needs to be written as type.GetTypeInfo().IsValueType

How can I call default(T) with a type?

For a reference type return null, for a value type, you could try using Activator.CreateInstance or calling the default constructor of the type.

public static object Default(Type type)
{
if(type.IsValueType)
{
return Activator.CreateInstance(type);
}

return null;
}

Equivalent of default(T) using reflection

Just check type.IsValueType; if that is true use Activator.CreateInstance(type) - otherwise it is null.

It also helps that you can pass null to SetValue on a PropertyInfo or FieldInfo and it will work for value-types with even for int, float etc.



Related Topics



Leave a reply



Submit