Attributes
You can gain control over which types and properties are serializable by adding attributes to your code.
The [TypeResolver]
attribute
This attribute lets you specify which type resolver to use during deserialization. A custom ITypeResolver
allows
you to fully control what's being serialized.
The [DefaultTypeResolver]
attribute
The default type resolver can serialize just about anything. The attributes below are understood by the default type resolver. You can add this attribute to the root class.
The attribute has an optional parameter that indicates which properties are to be serialized:
- IncludePropertiesWithBlueprintIncludeAttribute: includes all properties with a
[BlueprintInclude]
attribute. This flag is always set. - IncludePublicGetSetProperties: includes all properties with a public get and set method. This is the default value.
- IncludePublicGetProperties: includes all properties with a public get method.
[DefaultTypeResolver(IncludeProperties.IncludePublicGetSetProperties)]
public class Configuration
{
// ...
}
[BlueprintInclude]
and [BlueprintExclude]
These attributes include and exclude properties from serialization, respectively:
public class Configuration
{
[BlueprintExclude]
public string SecretProperty { get; set; }
[BlueprintInclude]
private string PublicProperty { get; set; }
}
By default, all properties with public get and set methods are included. The [BlueprintInclude]
property allows non-public properties to be serialized.
The [BlueprintConstructor]
attribute
This attribute allows you to match constructor parameters with properties. This allows you to serialize classes that have no default constructor, as long as each constructor parameter is explosed as a property with at least a get method:
[BlueprintConstructor("Name")]
public class Person
{
public Person(string name)
{
Name = name;
}
public string Name
{
get;
private set;
}
}
When serializing an instance of this class, the Name
property value is used, and during deserialization the value is passed to the constructor. Any
missing constructor parameters will cause deserialization to fail.
The [ValueDescriptor]
attribute
This attribute allows you to assign a custom IValueDescriptor
to your class, struct or enum:
[ValueDescriptor(typeof(CustomEnumDescriptor))]
public enum CustomEnum
{
Yes = 1,
No = 2,
}
public class CustomEnumDescriptor : IValueDescriptor
{
public Type Type { get { return typeof(CustomEnum); } }
public bool CanConvertFromString(string value)
{
switch (value)
{
case "Y":
case "N":
return true;
default:
return false;
}
}
public object ConvertFromString(string value)
{
switch (value)
{
case "Y":
return CustomEnum.Yes;
case "N":
return CustomEnum.No;
default:
throw new Exception("Invalid CustomEnum value '" + value + "'");
}
}
public string ConvertToString(object instance)
{
CustomEnum value = (CustomEnum)instance;
switch (value)
{
case CustomEnum.Yes:
return "Y";
case CustomEnum.No:
return "N";
default:
throw new Exception("Invalid CustomEnum value '" + value + "'");
}
}
}
The [DefaultValue]
attribute
This framework attribute is in the System.ComponentModel
namespace. When a property is set to its default value, it is not serialized.
Keep in mind that this default value is not automatically set to the property, you can set the default value in your constructor.