PeanutButter
Loading...
Searching...
No Matches
PeanutButter.DuckTyping.Extensions.DuckTypingObjectExtensions Class Reference

Provides a set of extension methods to enable duck-typing. More...

Static Public Member Functions

static bool CanDuckAs< T > (this object src)
 Tests whether or not a given object can be accurately duck-typed to the requested interface T. Accurate duck-typing requires exact matching of property names and types as well as method names and signatures.
static bool CanFuzzyDuckAs< T > (this object src)
 Tests whether or not a given object can be approximately duck-typed to the requested interface T. Fuzzy ducking will attempt to bridge when there are case mismatches in property and method names as well as attempting auto-conversion between underlying and exposed types (eg: a Guid property could be backed by an underlying string property) and will attempt parameter re-ordering when ducking methods, if required and possible.
static T DuckAs< T > (this object src)
 Attempts to accurately duck-type an object to an interface.
static T DuckAs< T > (this object src, bool throwOnError)
 Attempts to accurately duck-type an object to an interface.
static object FuzzyDuckAs (this object src, Type toType)
 Attempts to approximately duck-type an object to a a provided interface type.
static object FuzzyDuckAs (this object src, Type toType, bool throwOnError)
 Attempts to approximately duck-type an object to a a provided interface type.
static object DuckAs (this object src, Type toType)
 Attempts to accurately duck-type an object to a a provided interface type.
static object DuckAs (this object src, Type toType, bool throwOnError)
 Attempts to accurately duck-type an object to a a provided interface type.
static T FuzzyDuckAs< T > (this object src)
 Attempts to approximately duck-type an object to a a provided interface type.
static T FuzzyDuckAs< T > (this object src, bool throwOnError)
 Attempts to approximately duck-type an object to a a provided interface type.

Detailed Description

Provides a set of extension methods to enable duck-typing.

Member Function Documentation

◆ CanDuckAs< T >()

bool PeanutButter.DuckTyping.Extensions.DuckTypingObjectExtensions.CanDuckAs< T > ( this object src)
static

Tests whether or not a given object can be accurately duck-typed to the requested interface T. Accurate duck-typing requires exact matching of property names and types as well as method names and signatures.

Parameters
srcObject to inspect
Template Parameters
TDesired interface to duck to
Returns
True when the object can be accurately duck-typed; false otherwise. A false result here may not necessarily mean a false result from CanFuzzyDuckAs

◆ CanFuzzyDuckAs< T >()

bool PeanutButter.DuckTyping.Extensions.DuckTypingObjectExtensions.CanFuzzyDuckAs< T > ( this object src)
static

Tests whether or not a given object can be approximately duck-typed to the requested interface T. Fuzzy ducking will attempt to bridge when there are case mismatches in property and method names as well as attempting auto-conversion between underlying and exposed types (eg: a Guid property could be backed by an underlying string property) and will attempt parameter re-ordering when ducking methods, if required and possible.

Parameters
srcObject to inspect
Template Parameters
TDesired interface to duck to
Returns
True when the object can be approximately duck-typed; false otherwise

◆ DuckAs() [1/2]

object PeanutButter.DuckTyping.Extensions.DuckTypingObjectExtensions.DuckAs ( this object src,
Type toType )
static

Attempts to accurately duck-type an object to a a provided interface type.

Parameters
srcObject to duck-type
toTypeType of interface required
Returns
If accurate ducking is possible, this method returns a new object which wraps around the input object, exposing the properties and methods of the requested interface whilst calling through to the underlying object. If ducking cannot be achieved, this method silently returns null.

◆ DuckAs() [2/2]

object PeanutButter.DuckTyping.Extensions.DuckTypingObjectExtensions.DuckAs ( this object src,
Type toType,
bool throwOnError )
static

Attempts to accurately duck-type an object to a a provided interface type.

Parameters
srcObject to duck-type
toTypeType of interface required
throwOnErrorFlag to determine whether failure to duck should throw an exception
Returns
If accurate ducking is possible, this method returns a new object which wraps around the input object, exposing the properties and methods of the requested interface whilst calling through to the underlying object. If ducking cannot be achieved, this method silently returns null when throwOnError is false or throws an UnDuckableException when throwOnError is true.

◆ DuckAs< T >() [1/2]

Attempts to accurately duck-type an object to an interface.

Parameters
srcObject to duck-type
Template Parameters
TInterface required
Returns
If accurate ducking is possible, this method returns a new object which wraps around the input object, exposing the properties and methods of the requested interface whilst calling through to the underlying object. If ducking cannot be achieved, this method silently returns null.
Type Constraints
T :class 

◆ DuckAs< T >() [2/2]

T PeanutButter.DuckTyping.Extensions.DuckTypingObjectExtensions.DuckAs< T > ( this object src,
bool throwOnError )
static

Attempts to accurately duck-type an object to an interface.

Parameters
srcObject to duck-type
throwOnErrorFlag to allow throwing and exception when ducking cannot be achieved. The thrown exception contains information about what caused ducking to fail.
Template Parameters
TInterface required
Returns
If accurate ducking is possible, this method returns a new object which wraps around the input object, exposing the properties and methods of the requested interface whilst calling through to the underlying object. If ducking cannot be achieved, this method silently returns null when throwOnError is false or throws an UnDuckableException if throwOnError is true.
Type Constraints
T :class 

◆ FuzzyDuckAs() [1/2]

object PeanutButter.DuckTyping.Extensions.DuckTypingObjectExtensions.FuzzyDuckAs ( this object src,
Type toType )
static

Attempts to approximately duck-type an object to a a provided interface type.

Parameters
srcObject to duck-type
toTypeType of interface required
Returns
If approximate ducking is possible, this method returns a new object which wraps around the input object, exposing the properties and methods of the requested interface whilst calling through to the underlying object. If ducking cannot be achieved, this method silently returns null.

◆ FuzzyDuckAs() [2/2]

object PeanutButter.DuckTyping.Extensions.DuckTypingObjectExtensions.FuzzyDuckAs ( this object src,
Type toType,
bool throwOnError )
static

Attempts to approximately duck-type an object to a a provided interface type.

Parameters
srcObject to duck-type
toTypeType of interface required
throwOnErrorFlag to determine whether failure to duck should throw an exception
Returns
If approximate ducking is possible, this method returns a new object which wraps around the input object, exposing the properties and methods of the requested interface whilst calling through to the underlying object. If ducking cannot be achieved, this method silently returns null when throwOnError is false or throws an UnDuckableException when throwOnError is true.

◆ FuzzyDuckAs< T >() [1/2]

Attempts to approximately duck-type an object to a a provided interface type.

Parameters
srcObject to duck-type

<paramtype name="T">Type of interface required</paramtype>

Returns
If approximate ducking is possible, this method returns a new object which wraps around the input object, exposing the properties and methods of the requested interface whilst calling through to the underlying object. If ducking cannot be achieved, this method silently returns null.
Type Constraints
T :class 

◆ FuzzyDuckAs< T >() [2/2]

T PeanutButter.DuckTyping.Extensions.DuckTypingObjectExtensions.FuzzyDuckAs< T > ( this object src,
bool throwOnError )
static

Attempts to approximately duck-type an object to a a provided interface type.

Parameters
srcObject to duck-type
throwOnErrorFlag to determine whether failure to duck should throw an exception

<paramtype name="T">Type of interface required</paramtype>

Returns
If approximate ducking is possible, this method returns a new object which wraps around the input object, exposing the properties and methods of the requested interface whilst calling through to the underlying object. If ducking cannot be achieved, this method silently returns null when throwOnError is false or throws an UnDuckableException when throwOnError is true.
Type Constraints
T :class 

The documentation for this class was generated from the following file:
  • source/Utils/PeanutButter.DuckTyping/Extensions/DuckTypingObjectExtensions.cs