NamespaceSystemLibrary.Common.Net.Extensions
This class contains extension methods for Type
For instance: Inherits()
TypeExtensions
public static class TypeExtensions
Inheritance
System.Object
TypeExtensions
Methods
}
This class contains extension methods for Type
For instance: Inherits()
Returns a default instantiated value for value types, and null for reference types
Type | Name | Description |
---|---|---|
System.Type | type |
Type | Description |
---|---|
System.Object |
Returns the first generic type specified, or null
Optional: pass in higher index to return a different type argument
class Car
{
}
var type = typeof(List<Car>);
var genericType = type.GetTypeArgument();
// genericType is now 'typeof(Car)'
Type | Name | Description |
---|---|---|
System.Type | type | |
System.Int32 | index |
Type | Description |
---|---|
System.Type |
Returns all generic types found or null
class Car
{
}
var type = typeof(List<Car>);
var genericType = type.GetTypeArguments();
// genericType is now an array of Types with 1 item: 'typeof(Car)'
Type | Name | Description |
---|---|---|
System.Type | type |
Type | Description |
---|---|
System.Type[] |
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
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 | The type name |
Check if 'thisType' inherits or implements 'type
False if both types are the same
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 | 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 | true or false |
Returns true if internal else false
var t = typeof(Car);
var isInternal = t.IsInternal();
Type | Name | Description |
---|---|---|
System.Type | type |
Type | Description |
---|---|
System.Boolean | True or false |
Check if type is a KeyValuePair generic
Type | Name | Description |
---|---|---|
System.Type | type |
Type | Description |
---|---|
System.Boolean | true or false |
Check if type is a list or array
Does not check on IList, nor Dictionary, just 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 | true or false |
Set a static member on a Type, no matter if it is internal or private
Type | Name | Description |
---|---|---|
System.Type | type | |
System.String | memberName | |
System.Object | value |
}