.NET Portable TypeCast  3.1.0.4
A, easy-to-use tested, generic, portable, runtime-extensible, arbitrary type converter library
Core.TypeCast.ConverterMethodAttribute Class Reference

Use ConverterMethodAttribute to declare a method in an arbitrary class as a logical Converter function. The only restriction towards the attributed method is a maximum of two defined function parameters. This limitation is by design, to advocate the use of the single responsibility principle, and building complex converters out of smaller ones, as implemented by a different library. Take a look at the examples and documentation for more information. More...

+ Collaboration diagram for Core.TypeCast.ConverterMethodAttribute:

Public Member Functions

 ConverterMethodAttribute (bool isStatic=true, bool loadOnDemand=true, string name="", bool passInstance=false)
 Initializes a new instance of the ConverterMethodAttribute class, to declare a custom Converter function More...
 

Properties

Type BaseType [get, set]
 Gets or sets the base type. More...
 

Detailed Description

Use ConverterMethodAttribute to declare a method in an arbitrary class as a logical Converter function. The only restriction towards the attributed method is a maximum of two defined function parameters. This limitation is by design, to advocate the use of the single responsibility principle, and building complex converters out of smaller ones, as implemented by a different library. Take a look at the examples and documentation for more information.

The attribute provides collective initialization through the ConverterCollection class.

Example: Declaring an arbitrary class method to be loaded and encapsulated into a converter instance

```cs
[Converter(loadOnDemand: false, name: nameof(Program.BioCore.ToDNA))]
public class StringToDNA {
...
// Constructor with dependency injection
public StringToDNA(IConverterCollection collection)
{
... collection.Add((Func<string, DNA>)this.Convert); ...
}
// method added explicitly by the constructor supporting converter-dependency-injection
// Note: It is recommended to derive the class <tt>StringToDNA</tt> from <tt>ConverterCollectionDependency</tt>
public object Convert(object valueIn)
{
...
}
// method loaded automatically, through attribution
[ConverterMethod]
public DNA Convert(string valueIn)
{
...
}
...
}
```

Reference:

See also
Converter.Function, Converter.FunctionDefault

,

Definition at line 57 of file ConverterMethodAttribute.cs.

Constructor & Destructor Documentation

Core.TypeCast.ConverterMethodAttribute.ConverterMethodAttribute ( bool  isStatic = true,
bool  loadOnDemand = true,
string  name = "",
bool  passInstance = false 
)

Initializes a new instance of the ConverterMethodAttribute class, to declare a custom Converter function

Parameters
loadOnDemandSet to true to allow joint initialization by the lazy instancing of the ConverterCollection Core.Singleton
isStaticSet to true to invoke the method with this reference set to null, otherwise with an instance reference passed. Default is true.
nameAn alias as a string, ideally set via the nameof operator to identify among ambiguous converters.
dependencyInjectionWhether the declaring converter class is instantiated via dependency Injection.

Definition at line 68 of file ConverterMethodAttribute.cs.

69  {
70  this.LoadOnDemand = loadOnDemand;
71  this.Name = name;
72  this.IsStatic = isStatic;
73  this.PassInstance = passInstance;
74  }

Property Documentation

Type Core.TypeCast.ConverterMethodAttribute.BaseType
getset

Gets or sets the base type.

See also
Converter.BaseType

Definition at line 80 of file ConverterMethodAttribute.cs.


The documentation for this class was generated from the following file: