NamespaceSystemLibrary.Common.Net.Extensions
This class contains extension methods for Type
For instance: Inherits()
TypeExtensions
public static class TypeExtensions
Inheritance
System.Object
TypeExtensions
This class contains extension methods for Type
For instance: Inherits()
Type | Name | Description |
---|---|---|
System.Type | type |
Type | Description |
---|---|
System.Type |
Returns the type.Name
For generics such as List, Dictionary or Array, it returns the "inner type name" of those
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"
Type | Name | Description |
---|---|---|
System.Type | type |
Type | Description |
---|---|
System.String |
Check if 'thisType' inherits (implements) 'type'
Returns false if both types are the same or if 'type' is not inherited by 'thisType'
For interfaces this can be read as 'implements'
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
Type | Name | Description |
---|---|---|
System.Type | thisType | |
System.Type | type |
Type | Description |
---|---|
System.Boolean | Returns true or false |
Checks if type is a dictionary
var dictionary = new Dictionary<string, string>();
var result = dictionary.IsDictionary();
//result is true
Type | Name | Description |
---|---|---|
System.Type | type |
Type | Description |
---|---|
System.Boolean | Returns true or false |
Check if type is a list or array
var array = new string[] { "" };
var result = array.GetType().IsListOrArray();
//result is true
Type | Name | Description |
---|---|---|
System.Type | type |
Type | Description |
---|---|
System.Boolean | Returns true or false |
}