Skip to content

Getters

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

Getters

Introduction

The Getter class provides a way to control how a value for a cell is read during serialization. Getters are used in static serialization and in dynamic serialization if the type serialized is not "Dynamic Language Runtime (DLR) aware."

Supported Backers

Getter instances can be backed by methods, fields, and delegates. All backers directly return a value, there is no notion of failure.

Delegates

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

  • delegates of the form DesiredType Name(in WriteContext)
  • delegates of the form DesiredType Name(RowType, in WriteContext)

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

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

Fields

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

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

Methods

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

All supported methods must return a non-void value.

A static method must either:

  • have zero parameters or
  • have one parameter of the row type or
  • have one parameter of type in WriteContext or
  • have two parameters, the first of the row type and the second of in WriteContext

An instance method must:

  • be on the row type and have zero parameters
  • be on the row type and have a single parameter of in WriteContext

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

Properties

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

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

Explicit Casts

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