Table of Contents

Class TypeHelper

Namespace
Ecng.Common
Assembly
Ecng.Common.dll

Provides helper methods for working with types and related operations.

public static class TypeHelper
Inheritance
TypeHelper
Inherited Members

Properties

ApplicationName

Gets the name of the application.

public static string ApplicationName { get; }

Property Value

string

ApplicationNameWithVersion

Gets the name of the application with version.

public static string ApplicationNameWithVersion { get; }

Property Value

string

Methods

CheckOnNull<T>(T, string)

Checks if a value is null and throws an exception if it is.

public static T CheckOnNull<T>(this T value, string paramName = "value") where T : class

Parameters

value T

The value to check for null.

paramName string

The name of the parameter.

Returns

T

Returns the provided value if not null.

Type Parameters

T

The reference type to check.

CreateInstance(Type, params object[])

Creates an instance of the specified type using the given arguments.

public static object CreateInstance(this Type type, params object[] args)

Parameters

type Type

The type to instantiate.

args object[]

The constructor arguments.

Returns

object

Returns the created instance as an object.

CreateInstance<T>(Type, params object[])

Creates an instance of a specified type T using the given arguments.

public static T CreateInstance<T>(this Type type, params object[] args)

Parameters

type Type

The type to instantiate.

args object[]

The constructor arguments.

Returns

T

Returns the created instance as T.

Type Parameters

T

The type to instantiate.

CreateUnitialized(Type)

Creates an uninitialized instance of the specified type.

public static object CreateUnitialized(this Type type)

Parameters

type Type

The type to instantiate.

Returns

object

Returns the newly created uninitialized instance.

CreateUnitialized<TEntity>()

Creates an uninitialized instance of the specified generic type parameter.

public static TEntity CreateUnitialized<TEntity>()

Returns

TEntity

Returns the newly created uninitialized instance.

Type Parameters

TEntity

The type to instantiate.

DoDispose<TSource>(TSource)

Disposes the source if it implements IDisposable.

public static void DoDispose<TSource>(this TSource source)

Parameters

source TSource

The instance to dispose.

Type Parameters

TSource

The type of the source.

EnsureRunClass(Type)

Ensures the static constructor for a type is run.

public static void EnsureRunClass(this Type type)

Parameters

type Type

The type to use.

GenerateSalt(int)

Generates a salt byte array of the specified size.

public static byte[] GenerateSalt(int saltSize)

Parameters

saltSize int

The size of the salt array to create.

Returns

byte[]

Returns a byte array representing the salt.

GetDefaultValue(Type)

Gets the default value for the specified type.

public static object GetDefaultValue(this Type type)

Parameters

type Type

The type to get the default value for.

Returns

object

Returns the default value as an object.

GetPlatform(Type)

Retrieves the platform attribute for the specified type.

public static Platforms GetPlatform(this Type type)

Parameters

type Type

The type to inspect.

Returns

Platforms

Returns the platform value defined by TargetPlatformAttribute or AnyCPU.

GetTypeAsString(Type, bool)

Retrieves the type name as a string either in assembly-qualified form or not.

public static string GetTypeAsString(this Type type, bool isAssemblyQualifiedName)

Parameters

type Type

The type to convert.

isAssemblyQualifiedName bool

True to return the assembly-qualified name.

Returns

string

Returns the string representation of the type.

GetTypeName(Type, bool)

Gets the fully qualified name of a type or its assembly-qualified name.

public static string GetTypeName(this Type type, bool isAssemblyQualifiedName)

Parameters

type Type

The type to convert.

isAssemblyQualifiedName bool

Whether to return the assembly-qualified name.

Returns

string

Returns the string representation of the type name.

HasProperty(object, string)

Checks if a dynamic or regular object has a property with the specified name.

public static bool HasProperty(this object settings, string name)

Parameters

settings object

The object to inspect.

name string

The property name.

Returns

bool

Returns true if the property exists.

HiWord(int)

Extracts the high word from the integer.

public static int HiWord(this int iValue)

Parameters

iValue int

The integer value.

Returns

int

Returns the high word.

Is(Type, Type, bool)

Indicates whether a type is the specified base type or derives from it.

public static bool Is(this Type type, Type baseType, bool canSame = true)

Parameters

type Type

The type to compare.

baseType Type

The base type.

canSame bool

Allows checking if the types are the same.

Returns

bool

Returns true if type is or derives from the base type.

IsAttribute(Type)

Determines whether the specified type is an attribute.

public static bool IsAttribute(this Type type)

Parameters

type Type

The type to check.

Returns

bool

Returns true if the type is an attribute.

IsAutoGenerated(Type)

Determines if the specified type is generated by a compiler.

public static bool IsAutoGenerated(this Type type)

Parameters

type Type

The type to check.

Returns

bool

Returns true if the type has a CompilerGeneratedAttribute.

IsDelegate(Type)

Determines whether the specified type is a delegate.

public static bool IsDelegate(this Type type)

Parameters

type Type

The type to check.

Returns

bool

Returns true if the type is a delegate.

IsEnum(Type)

Determines whether the specified type is an enum.

public static bool IsEnum(this Type type)

Parameters

type Type

The type to check.

Returns

bool

Returns true if the type is an enum.

IsNet45OrNewer()

Determines if the current environment is .NET 4.5 or newer.

public static bool IsNet45OrNewer()

Returns

bool

Returns true if .NET 4.5 or newer is running.

IsNumeric(Type)

Determines if a type is numeric, including floating-point and decimal types.

public static bool IsNumeric(this Type type)

Parameters

type Type

The type to check.

Returns

bool

Returns true if the type is numeric.

IsNumericInteger(Type)

Determines if a type is an integer numeric type.

public static bool IsNumericInteger(this Type type)

Parameters

type Type

The type to check.

Returns

bool

Returns true if the type is an integer numeric type.

IsPrimitive(Type)

Determines whether a type is considered primitive, including common system types.

public static bool IsPrimitive(this Type type)

Parameters

type Type

The type to check.

Returns

bool

Returns true if the type is primitive or commonly handled as a primitive.

IsStruct(Type)

Determines whether the specified type is a struct.

public static bool IsStruct(this Type type)

Parameters

type Type

The type to check.

Returns

bool

Returns true if the type is a struct.

Checks if a string is a valid web link.

public static bool IsValidWebLink(this string link)

Parameters

link string

The link to check.

Returns

bool

Returns true if the link is a valid absolute URI.

Checks if a URI is a web-based link (http, https, or ftp).

public static bool IsWebLink(this Uri uri)

Parameters

uri Uri

The URI to check.

Returns

bool

Returns true if the URI is web-based.

Is<TBase>(Type, bool)

Indicates whether a type is the specified base type or derives from it.

public static bool Is<TBase>(this Type type, bool canSame = true)

Parameters

type Type

The type to compare.

canSame bool

Allows checking if the types are the same.

Returns

bool

Returns true if type is or derives from the base type.

Type Parameters

TBase

The base type to check.

LoWord(int)

Extracts the low word from the integer.

public static int LoWord(this int iValue)

Parameters

iValue int

The integer value.

Returns

int

Returns the low word.

Make(Type, IEnumerable<Type>)

Makes a generic type using a list of provided type arguments.

public static Type Make(this Type type, IEnumerable<Type> args)

Parameters

type Type

The generic type definition.

args IEnumerable<Type>

The type arguments as an IEnumerable.

Returns

Type

Returns the constructed generic type.

Make(Type, params Type[])

Makes a generic type using the provided type arguments.

public static Type Make(this Type type, params Type[] args)

Parameters

type Type

The generic type definition.

args Type[]

The type arguments.

Returns

Type

Returns the constructed generic type.

SingleOrAggr(IList<Exception>)

Returns either a single exception or an aggregate if there are multiple exceptions.

public static Exception SingleOrAggr(this IList<Exception> errors)

Parameters

errors IList<Exception>

A collection of exceptions.

Returns

Exception

Returns one exception or an AggregateException.

Throw(Exception)

Throws the specified exception while preserving the original stack trace.

public static void Throw(this Exception ex)

Parameters

ex Exception

The exception to throw.

ToScope<T>(T, bool)

Creates a scope for the specified value with optional ownership.

public static Scope<T> ToScope<T>(this T value, bool ownInstance = true)

Parameters

value T

The resource to be scoped.

ownInstance bool

Whether the scope owns the instance.

Returns

Scope<T>

Returns the newly created Scope.

Type Parameters

T

The type of the resource.