TypeHelper

Ecng.Common

Provides helper methods for working with types and related operations.

属性

ApplicationName
public static string ApplicationName { get; }
value = TypeHelper.ApplicationName

Gets the name of the application.

ApplicationNameWithVersion
public static string ApplicationNameWithVersion { get; }
value = TypeHelper.ApplicationNameWithVersion

Gets the name of the application with version.

方法

CheckOnNull``1
public static T CheckOnNull<T>(T value, string paramName)
result = TypeHelper.CheckOnNull(value, paramName)

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

value
The value to check for null.
paramName
The name of the parameter.

返回值: Returns the provided value if not null.

CreateInstance
public static object CreateInstance(Type type, object[] args)
result = TypeHelper.CreateInstance(type, args)

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

type
The type to instantiate.
args
The constructor arguments.

返回值: Returns the created instance as an object.

CreateInstance``1
public static T CreateInstance<T>(Type type, object[] args)
result = TypeHelper.CreateInstance(type, args)

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

type
The type to instantiate.
args
The constructor arguments.

返回值: Returns the created instance as T.

DoDispose``1
public static void DoDispose<TSource>(TSource source)
TypeHelper.DoDispose(source)

Disposes the source if it implements IDisposable.

source
The instance to dispose.
EnsureRunClass
public static void EnsureRunClass(Type type)
TypeHelper.EnsureRunClass(type)

Ensures the static constructor for a type is run.

type
The type to use.
EnterScope
public static Scope EnterScope(ISynchronizable sync)
result = TypeHelper.EnterScope(sync)

Enters a synchronized scope for thread-safe operations on the ISynchronizable.

sync
ISynchronizable

返回值: A Scope that represents the synchronized scope.

GenerateSalt
public static byte[] GenerateSalt(int saltSize)
result = TypeHelper.GenerateSalt(saltSize)

Generates a salt byte array of the specified size.

saltSize
The size of the salt array to create.

返回值: Returns a byte array representing the salt.

GetDefaultValue
public static object GetDefaultValue(Type type)
result = TypeHelper.GetDefaultValue(type)

Gets the default value for the specified type.

type
The type to get the default value for.

返回值: Returns the default value as an object.

GetPlatform
public static Platforms GetPlatform(Type type)
result = TypeHelper.GetPlatform(type)

Retrieves the platform attribute for the specified type.

type
The type to inspect.

返回值: Returns the platform value defined by TargetPlatformAttribute or AnyCPU.

GetTypeAsString
public static string GetTypeAsString(Type type, bool isAssemblyQualifiedName)
result = TypeHelper.GetTypeAsString(type, isAssemblyQualifiedName)

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

type
The type to convert.
isAssemblyQualifiedName
True to return the assembly-qualified name.

返回值: Returns the string representation of the type.

GetTypeName
public static string GetTypeName(Type type, bool isAssemblyQualifiedName)
result = TypeHelper.GetTypeName(type, isAssemblyQualifiedName)

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

type
The type to convert.
isAssemblyQualifiedName
Whether to return the assembly-qualified name.

返回值: Returns the string representation of the type name.

HasProperty
public static bool HasProperty(object settings, string name)
result = TypeHelper.HasProperty(settings, name)

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

settings
The object to inspect.
name
The property name.

返回值: Returns true if the property exists.

HiWord
public static int HiWord(int iValue)
result = TypeHelper.HiWord(iValue)

Extracts the high word from the integer.

iValue
The integer value.

返回值: Returns the high word.

Is
public static bool Is(Type type, Type baseType, bool canSame)
result = TypeHelper.Is(type, baseType, canSame)

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

type
The type to compare.
baseType
The base type.
canSame
Allows checking if the types are the same.

返回值: Returns true if type is or derives from the base type.

IsAttribute
public static bool IsAttribute(Type type)
result = TypeHelper.IsAttribute(type)

Determines whether the specified type is an attribute.

type
The type to check.

返回值: Returns true if the type is an attribute.

IsAutoGenerated
public static bool IsAutoGenerated(Type type)
result = TypeHelper.IsAutoGenerated(type)

Determines if the specified type is generated by a compiler.

type
The type to check.

返回值: Returns true if the type has a CompilerGeneratedAttribute.

IsDelegate
public static bool IsDelegate(Type type)
result = TypeHelper.IsDelegate(type)

Determines whether the specified type is a delegate.

type
The type to check.

返回值: Returns true if the type is a delegate.

IsEnum
public static bool IsEnum(Type type)
result = TypeHelper.IsEnum(type)

Determines whether the specified type is an enum.

type
The type to check.

返回值: Returns true if the type is an enum.

IsNumeric
public static bool IsNumeric(Type type)
result = TypeHelper.IsNumeric(type)

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

type
The type to check.

返回值: Returns true if the type is numeric.

IsNumericInteger
public static bool IsNumericInteger(Type type)
result = TypeHelper.IsNumericInteger(type)

Determines if a type is an integer numeric type.

type
The type to check.

返回值: Returns true if the type is an integer numeric type.

IsPrimitive
public static bool IsPrimitive(Type type)
result = TypeHelper.IsPrimitive(type)

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

type
The type to check.

返回值: Returns true if the type is primitive or commonly handled as a primitive.

IsStruct
public static bool IsStruct(Type type)
result = TypeHelper.IsStruct(type)

Determines whether the specified type is a struct.

type
The type to check.

返回值: Returns true if the type is a struct.

Is``1
public static bool Is<TBase>(Type type, bool canSame)
result = TypeHelper.Is(type, canSame)

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

type
The type to compare.
canSame
Allows checking if the types are the same.

返回值: Returns true if type is or derives from the base type.

LoWord
public static int LoWord(int iValue)
result = TypeHelper.LoWord(iValue)

Extracts the low word from the integer.

iValue
The integer value.

返回值: Returns the low word.

Make
public static Type Make(Type type, Type[] args)
result = TypeHelper.Make(type, args)

Makes a generic type using the provided type arguments.

type
The generic type definition.
args
The type arguments.

返回值: Returns the constructed generic type.

Make
public static Type Make(Type type, IEnumerable<Type> args)
result = TypeHelper.Make(type, args)

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

type
The generic type definition.
args
The type arguments as an IEnumerable.

返回值: Returns the constructed generic type.

SingleOrAggr
public static Exception SingleOrAggr(IList<Exception> errors)
result = TypeHelper.SingleOrAggr(errors)

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

errors
A collection of exceptions.

返回值: Returns one exception or an AggregateException.

SizeOf
public static int SizeOf(Type type)
result = TypeHelper.SizeOf(type)

Returns the size in bytes of the specified unmanaged type.

type
The type whose size is to be computed.

返回值: The size in bytes of the specified type.

SizeOf``1
public static int SizeOf<T>()
result = TypeHelper.SizeOf()

Returns the size in bytes of an unmanaged type T.

返回值: The size in bytes of the specified type.

ToScope``1
public static Scope<T> ToScope<T>(T value, bool ownInstance)
result = TypeHelper.ToScope(value, ownInstance)

Creates a scope for the specified value with optional ownership.

value
The resource to be scoped.
ownInstance
Whether the scope owns the instance.

返回值: Returns the newly created Scope.