System Library Common Framework
Search Results for

    *.Benchmark*,*.Tests*,*.ApiTests*,*.BaseTest*,DelegateJsonConverter,CalleeCancelledRequestException,ClientResponse,ContentType,OutputCachePolicy
    Show / Hide Table of Contents Show / Hide Table of Contents
    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
    Methods arguments
    Type Name Description
    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

    public static Type GetTypeArgument(this Type type, int index = 0)

    X
    class Car
    {
    }
    
    var type = typeof(List<Car>);
    var genericType = type.GetTypeArgument();
    // genericType is now 'typeof(Car)'
    X
    Methods arguments
    Type Name Description
    Type type
    int index
    X
    Type Description
    Type

    Returns all generic types found or null

    public static Type[] GetTypeArguments(this Type type)

    X
    class Car
    {
    }
    
    var type = typeof(List<Car>);
    var genericType = type.GetTypeArguments();
    // genericType is now an array of Types with 1 item: 'typeof(Car)'
    X
    Methods arguments
    Type Name Description
    Type type
    X
    Type Description
    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

    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
    Methods arguments
    Type Name Description
    Type type
    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
    Methods arguments
    Type Name Description
    Type thisType
    Type type
    X
    Type Description
    bool

    true or false

    Returns true if type is an anonymous type else false

    public static bool IsAnonymousType(this Type type)

    X
    Methods arguments
    Type Name Description
    Type type
    X
    Type Description
    bool

    Retruns true if the Type is a normal Class

    public static bool IsClassType(this Type type)

    X
    Methods arguments
    Type Name Description
    Type type
    X
    Type Description
    bool

    Checks if type is a dictionary

    public static bool IsDictionary(this Type type)

    X
    var dictionary = new Dictionary<string, string>();
    
    var result = dictionary.IsDictionary();
    //result is true
    X
    Methods arguments
    Type Name Description
    Type type
    X
    Type Description
    bool

    true or false

    Returns true if internal else false

    public static bool IsInternal(this Type type)

    X
    var t = typeof(Car);
    var isInternal = t.IsInternal();
    X
    Methods arguments
    Type Name Description
    Type type
    X
    Type Description
    bool

    True or false

    Check if type is a KeyValuePair generic

    public static bool IsKeyValuePair(this Type type)

    X
    Methods arguments
    Type Name Description
    Type type
    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

    public static bool IsListOrArray(this Type type)

    X
    var array = new string[] { "" };
    
    var result = array.GetType().IsListOrArray();
    //result is true
    X
    Methods arguments
    Type Name Description
    Type type
    X
    Type Description
    bool

    true or false

    Returns true if type is nullable

    public static bool IsNullableType(this Type type)

    X
    Methods arguments
    Type Name Description
    Type type
    X
    Type Description
    bool

    Returns true if type is a number type, such as int, long, etc...

    public static bool IsNumberType(this Type type)

    X
    Methods arguments
    Type Name Description
    Type type
    X
    Type Description
    bool

    Set a static member on a Type, no matter if it is internal or private

    public static void SetStaticMember(this Type type, string memberName, object value)

    X
    Methods arguments
    Type Name Description
    Type type
    string memberName
    object value

    }

    In This Article
    Package: nuget
    Source: github
    Website: System Library Common Framework