Class TypeExtensions
Type extensions
Inherited Members
Namespace: XData.Extensions
Assembly: XData.docfx.dll
Syntax
public static class TypeExtensions
Methods
| Improve this Doc View SourceArrayCast(Type, ICollection)
Cast object array to specific type array
Declaration
public static object ArrayCast(this Type elementType, ICollection collection)
Parameters
Type | Name | Description |
---|---|---|
Type | elementType | Type of result array elements |
ICollection | collection | Source collection |
Returns
Type | Description |
---|---|
Object | Typed array |
ChangeType(Object, Type)
Type convert
Declaration
public static object ChangeType(this object value, Type conversion)
Parameters
Type | Name | Description |
---|---|---|
Object | value | Value to convert |
Type | conversion | Target type |
Returns
Type | Description |
---|---|
Object | Target converted value |
ChangeType<T>(Object)
Type convert
Declaration
public static T ChangeType<T>(this object value)
Parameters
Type | Name | Description |
---|---|---|
Object | value | Value to convert |
Returns
Type | Description |
---|---|
T | Target converted value |
Type Parameters
Name | Description |
---|---|
T | Target type |
ConvertTo<T>(Enum)
Convert enumeration value to another enumeration by hash code
Declaration
public static T ConvertTo<T>(this Enum source)
Parameters
Type | Name | Description |
---|---|---|
Enum | source | Source enumeration value |
Returns
Type | Description |
---|---|
T | Target enumeration value |
Type Parameters
Name | Description |
---|---|
T | Target enumeration type |
EqualValue(Enum, Enum)
Compare Enumeration values by hash codes
Declaration
public static bool EqualValue(this Enum source, Enum other)
Parameters
Type | Name | Description |
---|---|---|
Enum | source | Source enumeration value |
Enum | other | Other enumeration value |
Returns
Type | Description |
---|---|
Boolean | Values are equal |
GetDefault(Type)
Get type default value
Declaration
public static object GetDefault(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | Type |
Returns
Type | Description |
---|---|
Object | Default value |
GetDefault<TType>()
Get type default value
Declaration
public static object GetDefault<TType>()
Returns
Type | Description |
---|---|
Object | Default value |
Type Parameters
Name | Description |
---|---|
TType | Type |
GetEnumKeyValuePairs(Type)
Declaration
public static IEnumerable<KeyValuePair<long, string>> GetEnumKeyValuePairs(this Type enumType)
Parameters
Type | Name | Description |
---|---|---|
Type | enumType |
Returns
Type | Description |
---|---|
IEnumerable<KeyValuePair<Int64, String>> |
GetEnumValue(Type, Int64)
Returns enum id and value pair for specified id
Declaration
public static KeyValuePair<long, string>? GetEnumValue(this Type enumType, long id)
Parameters
Type | Name | Description |
---|---|---|
Type | enumType | Enum type |
Int64 | id | Enum key |
Returns
Type | Description |
---|---|
Nullable<KeyValuePair<Int64, String>> | Enum id and value pair |
GetGenericMethod(Type, String, IEnumerable<Type>)
Returns generic method with typed arguments
Declaration
public static MethodInfo GetGenericMethod(this Type type, string name, IEnumerable<Type> parameterTypes)
Parameters
Type | Name | Description |
---|---|---|
Type | type | Method type |
String | name | Method name |
IEnumerable<Type> | parameterTypes | Parameter types |
Returns
Type | Description |
---|---|
MethodInfo | Reflection method info |
Exceptions
Type | Condition |
---|---|
XDataRuntimeException | Method name can not be null or empty |
XDataRuntimeException | Method parameter types can not be null |
GetLocalizedEnumKeyValuePairs(Type)
Returns id and name pairs for all enum values
Declaration
public static IEnumerable<KeyValuePair<long, string>> GetLocalizedEnumKeyValuePairs(this Type enumType)
Parameters
Type | Name | Description |
---|---|---|
Type | enumType | Enum type |
Returns
Type | Description |
---|---|
IEnumerable<KeyValuePair<Int64, String>> | Sequence of id and name pairs for all enum values |
GetLocalizedEnumValue(Type, Int64)
Returns enum id and localized value pair for specified id
Declaration
public static KeyValuePair<long, string>? GetLocalizedEnumValue(this Type enumType, long id)
Parameters
Type | Name | Description |
---|---|---|
Type | enumType | Enum type |
Int64 | id | Enum key |
Returns
Type | Description |
---|---|
Nullable<KeyValuePair<Int64, String>> | Enum id and localized value pair |
GetRealType(Type)
Get Generic base type
Declaration
public static Type GetRealType(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | Type |
Returns
Type | Description |
---|---|
Type | Base type |
Implements<T>(Type, T)
Test type implements an interface
Declaration
public static bool Implements<T>(this Type type, T interfaceType)
where T : Type
Parameters
Type | Name | Description |
---|---|---|
Type | type | Type |
T | interfaceType | Interface |
Returns
Type | Description |
---|---|
Boolean | Type is implements an interface |
Type Parameters
Name | Description |
---|---|
T | Interface type |
InvokeMethod(Object, Expression<Action>, BindingFlags)
Invoke method with replacing base object type wrapper
Declaration
public static void InvokeMethod(this object obj, Expression<Action> expr, BindingFlags bindingFlags = BindingFlags.Default)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | Object to invoke method with |
Expression<Action> | expr | Call expression |
BindingFlags | bindingFlags | Binding flags |
Exceptions
Type | Condition |
---|---|
XDataRuntimeException |
InvokeMethod(Object, Type[], Expression<Action>, BindingFlags)
Invoke generic method wrapper
Declaration
public static void InvokeMethod(this object obj, Type[] types, Expression<Action> expr, BindingFlags bindingFlags = BindingFlags.Default)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | Object to invoke method (set null to call static method) |
Type[] | types | Generic arguments types array |
Expression<Action> | expr | Call expression (set generic arguments to any valid types) |
BindingFlags | bindingFlags | Binding flags |
Exceptions
Type | Condition |
---|---|
XDataRuntimeException |
InvokeMethod<TRet>(Object, Expression<Func<TRet>>, BindingFlags)
Invoke method with replacing base object type wrapper
Declaration
public static TRet InvokeMethod<TRet>(this object obj, Expression<Func<TRet>> expr, BindingFlags bindingFlags = BindingFlags.Default)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | Object to invoke method with |
Expression<Func<TRet>> | expr | Call expression |
BindingFlags | bindingFlags | Binding flags |
Returns
Type | Description |
---|---|
TRet | Call return value |
Type Parameters
Name | Description |
---|---|
TRet | Return value type |
Exceptions
Type | Condition |
---|---|
XDataRuntimeException |
InvokeMethod<TRet>(Object, Type[], Expression<Func<TRet>>, BindingFlags)
Invoke generic method wrapper
Declaration
public static TRet InvokeMethod<TRet>(this object obj, Type[] types, Expression<Func<TRet>> expr, BindingFlags bindingFlags = BindingFlags.Default)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | Object to invoke method (set null to call static method) |
Type[] | types | Generic arguments types array |
Expression<Func<TRet>> | expr | Call expression (set generic arguments to any valid types) |
BindingFlags | bindingFlags | Binding flags |
Returns
Type | Description |
---|---|
TRet | Call return value |
Type Parameters
Name | Description |
---|---|
TRet | Return value type |
Exceptions
Type | Condition |
---|---|
XDataRuntimeException |
IsBasedOn<TType>(Type)
Check generic base type
Declaration
public static bool IsBasedOn<TType>(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | Generic type |
Returns
Type | Description |
---|---|
Boolean | Generic type is based on TType |
Type Parameters
Name | Description |
---|---|
TType | Base type to check |
IsBinary(Type)
Check Lob type
Declaration
public static bool IsBinary(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | Type to check |
Returns
Type | Description |
---|---|
Boolean | Type is binary |
IsEnum(Type)
Check Enum type
Declaration
public static bool IsEnum(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | Type to check |
Returns
Type | Description |
---|---|
Boolean | Type is binary |
IsNullable(Type)
Check nullable type
Declaration
public static bool IsNullable(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | Type to check |
Returns
Type | Description |
---|---|
Boolean | Type is nullable |
IsXml(Type)
Check Xml type
Declaration
public static bool IsXml(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | Type to check |
Returns
Type | Description |
---|---|
Boolean | Type is binary |
NullWrap<T>(T)
Null value wrapper
Declaration
public static T NullWrap<T>(T source)
where T : class, new()
Parameters
Type | Name | Description |
---|---|---|
T | source | Source |
Returns
Type | Description |
---|---|
T | Source or empty object of source type |
Type Parameters
Name | Description |
---|---|
T | Source type |
NullWrap<T>(T[])
Null value wrapper
Declaration
public static T[] NullWrap<T>(T[] source)
Parameters
Type | Name | Description |
---|---|---|
T[] | source | Source array |
Returns
Type | Description |
---|---|
T[] | Source or empty array |
Type Parameters
Name | Description |
---|---|
T | Array element type |
ParseEnum(Type, String)
Returns id for specified enum type and string value
Declaration
public static long? ParseEnum(this Type enumType, string value)
Parameters
Type | Name | Description |
---|---|---|
Type | enumType | Enum type |
String | value | String value of enum |
Returns
Type | Description |
---|---|
Nullable<Int64> | Enum id |
ParseEnumRange(Type, ICollection<Object>)
Parse enum values range
Declaration
public static object[] ParseEnumRange(this Type enumType, ICollection<object> values)
Parameters
Type | Name | Description |
---|---|---|
Type | enumType | Enum type |
ICollection<Object> | values | Enum values range |
Returns
Type | Description |
---|---|
Object[] | Enum id array |
See Also
| Improve this Doc View SourceParseType(String)
Parse type full name
Declaration
public static Type ParseType(this string typeFullName)
Parameters
Type | Name | Description |
---|---|---|
String | typeFullName |
Returns
Type | Description |
---|---|
Type |
TryConvert<T, TResult>(T, Func<T, TResult>)
Try to convert value
Declaration
public static TResult TryConvert<T, TResult>(this T source, Func<T, TResult> convert)
where T : class
Parameters
Type | Name | Description |
---|---|---|
T | source | Source value |
Func<T, TResult> | convert | Conversion logic |
Returns
Type | Description |
---|---|
TResult | Converted value or default of TResult |
Type Parameters
Name | Description |
---|---|
T | Source type |
TResult | Result type |
TryConvert<T, TResult>(T, Func<T, TResult>, TResult)
Try to convert value
Declaration
public static TResult TryConvert<T, TResult>(this T source, Func<T, TResult> convert, TResult defaultValue)
where T : class
Parameters
Type | Name | Description |
---|---|---|
T | source | Source value |
Func<T, TResult> | convert | Conversion logic |
TResult | defaultValue | Default result value |
Returns
Type | Description |
---|---|
TResult | Converted value or default of TResult |
Type Parameters
Name | Description |
---|---|
T | Source type |
TResult | Result type |