Skip to content

Setters

kevin-montrose edited this page Apr 10, 2021 · 6 revisions

Setters

Introduction

The Setter class provides a way to control how a value read from a cell is assigned to a member during deserialization. Setters are used during static and dynamic deserialization.

Supported Backers

Setter instances can be backed by methods, fields, constructor parameters, and delegates. All backers directly take a value, there is no notion of failure.

Constructor Parameters

A Setter can be backed by a parameter to a constructor, provided that the constructor is also used to back the InstanceProvider returned by the associated ITypeDescriber.

Use the Setter.ForConstructorParameter(ParameterInfo) method to create a Setter backed by a constructor parameter. You can also explicitly cast any ParameterInfo to a Setter.

Delegates

A Setter may be backed by two different types of delegates:

  • delegates of the form void Name(Value, in ReadContext)
  • delegates of the form void Name(RowType, ValueType, in ReadContext)

They differ by whether or not they take the row being deserialized as a parameter.

Cesil provides StaticSetterDelegate<TValue>, SetterDelegate<TRow, TValue> and Setter.ForDelegate<...>(...)) overloads which conform to these forms. You can also explicitly cast any delegate to a Setter, provided it is logically equivalent to StaticSetterDelegate<TValue> or SetterDelegate<TRow, TValue>.

Fields

Any FieldInfo may back a Setter. If it is an instance field, it must be on the row type or a type it is assignable to.

Use the Setter.ForField(FieldInfo) method to create a Setter backed by a field. You can also explicitly cast any FieldInfo to a Setter.

Methods

A Setter can be backed by many different kinds of MethodInfo.

All supported methods must have a void return.

A static method must do one of the following:

  • take a single parameter of the value or
  • take two parameters, the first being the row and the second being the value or
  • take two parameters, the first being the value and the second being an in ReadContext or
  • take three parameters, the first being the row, the second being the value, and the third being a in ReadContext

An instance method must do one of the following:

  • be on the row type and take a single parameter of the value
  • be on the row type and take two parameters, the value and an in ReadContext

Use the Setter.ForMethod(MethodInfo) method to create a Setter backed by a method. You can also explicitly cast any MethodInfo to a Setter, provided it follows the above rules.

Properties

There is a convenience method of Setter.ForProperty(PropertyInfo) for backing a Setter with a property.

It is logically equivalent to passing a property's set method to Setter.ForMethod(MethodInfo).

Explicit Casts

Cesil provides explicit casts of MethodInfo, FieldInfo, ParameterInfo, and Delegate to Setter. These are roughly equivalent to calling the static ForXXX(...) methods on Setter, but differ by allowing null values.