Pathfinder  v3.0
The definitive Hacknet programmable modding system
Pathfinder.Util.ConvertAny Class Reference

Extended conversion utilities to convert any type to any other type provided the conversion is valid. More...

Static Public Member Functions

static bool CanConvert< ToType > (object value)
 Determines whether this instance can convert the specified value. More...
 
static bool CanConvert (Type fromType, Type toType)
 Determines whether this instance can convert the specified fromType to the toType . More...
 
static bool TryConvert< ToType > (object value, out ToType toValue)
 Attempts to convert the value to the specified type. More...
 
static ToType Convert< ToType > (object fromValue)
 Converts the specified from value. More...
 
static object ConvertByType (object fromValue, Type toType)
 Converts value to the specified type. More...
 
static T Default< T > ()
 Creates a default instance of T More...
 
static object DefaultByType (Type type)
 Creates a default instance of type . More...
 

Detailed Description

Member Function Documentation

◆ CanConvert()

static bool Pathfinder.Util.ConvertAny.CanConvert ( Type  fromType,
Type  toType 
)
static
Parameters
fromTypeFrom type.
toTypeTo type.
Returns
true if the conversion is valid; otherwise, false.

This method checks 4 types of conversions:

  1. If they are the same type, returns true.
  2. If they are both IConvertible types, return true.
  3. If the toType is part of the inheritance chain of fromType.
  4. Direct casting, only returns true if both types have default constructors.

◆ CanConvert< ToType >()

static bool Pathfinder.Util.ConvertAny.CanConvert< ToType > ( object  value)
static
Template Parameters
ToTypeThe type of to type.
Parameters
valueFrom value.
Returns
true if this instance can convert the specified from value; otherwise, false.

◆ Convert< ToType >()

static ToType Pathfinder.Util.ConvertAny.Convert< ToType > ( object  fromValue)
static
Template Parameters
ToTypeThe type to convert to.
Parameters
fromValueThe value to convert.
Returns
Converted value.
Exceptions
InvalidCastExceptionRaised when the value cannot be converted to the specified type.

◆ ConvertByType()

static object Pathfinder.Util.ConvertAny.ConvertByType ( object  fromValue,
Type  toType 
)
static
Parameters
fromValueThe value to convert.
toTypeThe type to convert to.
Returns
Object that represents the converted type.

◆ Default< T >()

static T Pathfinder.Util.ConvertAny.Default< T > ( )
static
Template Parameters
TType to create a default instance of.
Returns
Default instance of T .

For primitive types this will create a value of 0 with the specified type. For objects, if the object has a default constructor it will return a default instance of the type. If the object does not have a default constructor, it will return null.

◆ DefaultByType()

static object Pathfinder.Util.ConvertAny.DefaultByType ( Type  type)
static
Parameters
typeThe type.
Returns
Instance of the specified type.

This method uses the generic Default method to create an instance of the type.

◆ TryConvert< ToType >()

static bool Pathfinder.Util.ConvertAny.TryConvert< ToType > ( object  value,
out ToType  toValue 
)
static
Template Parameters
ToTypeThe type to convert to.
Parameters
valueValue to convert.
toValueConverted value.
Returns
true if conversion succeeds, false otherwise.

If the conversion fails, the value of toValue is the default of that particular type.


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