NamespaceSystemLibrary.Common.Framework.Extensions SystemLibrary.Common.Framework.Net.dll
This class contains extension methods for Type
For instance: Inherits()
TypeExtensions
public static class TypeExtensions
Inheritance
object
TypeExtensions
Examples
Arguments
X
Methods
Returns a default instantiated value for value types, and null for reference types
public static object Default(this Type type)
X
Type | Description |
---|---|
object |
Returns the first generic type specified, or null
Optional: pass in higher index to return a different type argument
Returns the Name of the Type that makes 'most sense'
For generics, such as a IList, List, Dictionary, it will return the Name of the first generic Type specified
public static string GetTypeName(this Type type)
X
class Car
{
}
var result = typeof(Car).GetTypeName();
// result is "Car"
var list = new List<Car>
var result = list.GetType().GetTypeName();
// result is "Car"
var result = typeof(List<Car>).GetTypeName();
// result is "Car"
var result = typeof(Car[]).GetTypeName();
// result is "Car"
X
Type | Description |
---|---|
string | The type name |
Check if 'thisType' inherits or implements 'type
False if both types are the same
public static bool Inherits(this Type thisType, Type type)
X
class Car : IVehicle
{
}
var result = typeof(Car).Inherits(typeof(IVehicle));
// result is true, as it inherits/implements IVehicle
var result = typeof(Car).Inherits(typeof(Car));
// result is false, as Car cannot inherit/implement itself
X
Type | Description |
---|---|
bool | true or false |
Check if type is a list or array
Remarks
X
Does not check on IList, nor Dictionary, just List or Array
}