Class TypeExtensions
- Namespace
- Cuemon.Extensions
- Assembly
- Cuemon.Extensions.Core.dll
Extension methods for the Type class.
public static class TypeExtensions
- Inheritance
-
TypeExtensions
Examples
TypeExtensions provides extension methods for Type that simplify classification and metadata retrieval via methods like ToFriendlyName, ToTypeCode, and various Has*/Is* predicates. This example inspects IList<string>, ConcurrentDictionary<string, int>, StringComparer, int?, and anonymous types, calling HasEnumerableImplementation, HasDictionaryImplementation, IsNullable, IsComplex, GetDefaultValue, and ToFriendlyName. It also demonstrates HasAnonymousCharacteristics on an anonymous object, HasTypes for hierarchy checks, HasInterfaces for generic interface matching, and HasAttributes for attribute presence. Console output prints friendly type names like IList<String>, boolean capability flags, and the default value 0 for typeof(int).
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using Cuemon.Extensions;
namespace MyApp.Examples;
public static class TypeExtensionsExample
{
public static void Demonstrate()
{
Type listType = typeof(IList<string>);
Type dictionaryType = typeof(ConcurrentDictionary<string, int>);
Type comparerType = typeof(StringComparer);
Type nullableInt = typeof(int?);
Console.WriteLine(listType.ToFriendlyName());
Console.WriteLine(listType.ToFriendlyName(options => options.FullName = true));
Console.WriteLine(typeof(string).ToTypeCode());
Console.WriteLine(listType.HasEnumerableImplementation());
Console.WriteLine(typeof(string).HasComparableImplementation());
Console.WriteLine(dictionaryType.HasDictionaryImplementation());
Console.WriteLine(comparerType.HasEqualityComparerImplementation());
Console.WriteLine(comparerType.HasComparerImplementation());
Console.WriteLine(typeof(KeyValuePair<string, int>).HasKeyValuePairImplementation());
Console.WriteLine(nullableInt.IsNullable());
Console.WriteLine(typeof(Stream).IsComplex());
Console.WriteLine(typeof(int).IsSimple());
Console.WriteLine(typeof(int).GetDefaultValue());
Console.WriteLine(typeof(FileStream).HasTypes(typeof(Stream)));
Console.WriteLine(typeof(List<>).HasInterfaces(typeof(IEnumerable<>)));
Console.WriteLine(typeof(string).HasAttributes(typeof(SerializableAttribute)));
Console.WriteLine(new { Name = "sample", Value = 42 }.GetType().HasAnonymousCharacteristics());
}
}
Methods
GetDefaultValue(Type)
Gets the default value of the specified type.
public static object GetDefaultValue(this Type type)
Parameters
Returns
- object
The default value of
type.
HasAnonymousCharacteristics(Type)
Determines whether the specified type suggest an anonymous implementation (be that in a form of a type, delegate or lambda expression).
public static bool HasAnonymousCharacteristics(this Type type)
Parameters
Returns
- bool
trueif the specifiedtypesuggest an anonymous implementation; otherwise,false.
Remarks
If you can avoid it, don't use this method. It is - to say the least - fragile.
Exceptions
- ArgumentNullException
typecannot be null.
HasAttributes(Type, params Type[])
Determines whether the specified type contains one or more of the specified attributeTypes.
public static bool HasAttributes(this Type type, params Type[] attributeTypes)
Parameters
typeTypeThe Type to extend.
attributeTypesType[]The attribute target types to be matched against.
Returns
- bool
trueif the specifiedtypecontains one or more of the specifiedattributeTypes; otherwise,false.
Exceptions
- ArgumentNullException
typecannot be null -or-attributeTypescannot be null.
HasComparableImplementation(Type)
Determines whether the specified type implements either IComparable or IComparable<T>.
public static bool HasComparableImplementation(this Type type)
Parameters
Returns
- bool
trueif the specifiedtypeimplements either IComparable or IComparable<T>; otherwise,false.
Exceptions
- ArgumentNullException
typecannot be null.
HasComparerImplementation(Type)
Determines whether the specified type implements either IComparer or IComparer<T>.
public static bool HasComparerImplementation(this Type type)
Parameters
Returns
- bool
trueif the specifiedtypeimplements either IComparer or IComparer<T>; otherwise,false.
Exceptions
- ArgumentNullException
typecannot be null.
HasDictionaryImplementation(Type)
Determines whether the specified type implements either IDictionary, IDictionary<TKey, TValue> or IReadOnlyDictionary<TKey, TValue>.
public static bool HasDictionaryImplementation(this Type type)
Parameters
Returns
- bool
trueif the specifiedtypeimplements either IDictionary, IDictionary<TKey, TValue> or IReadOnlyDictionary<TKey, TValue>; otherwise,false.
Exceptions
- ArgumentNullException
typecannot be null.
HasEnumerableImplementation(Type)
Determines whether the specified type implements either IEnumerable or IEnumerable<T>.
public static bool HasEnumerableImplementation(this Type type)
Parameters
Returns
- bool
trueif the specifiedtypeimplements either IEnumerable or IEnumerable<T>; otherwise,false.
Exceptions
- ArgumentNullException
typecannot be null.
HasEqualityComparerImplementation(Type)
Determines whether the specified type implements either IEqualityComparer or IEqualityComparer<T>.
public static bool HasEqualityComparerImplementation(this Type type)
Parameters
Returns
- bool
trueif the specifiedtypeimplements either IEqualityComparer or IEqualityComparer<T>; otherwise,false.
Exceptions
- ArgumentNullException
typecannot be null.
HasInterfaces(Type, params Type[])
Determines whether the specified type contains one or more of the target interfaces specified throughout this member's inheritance chain.
public static bool HasInterfaces(this Type type, params Type[] interfaceTypes)
Parameters
typeTypeThe Type to extend.
interfaceTypesType[]The target interface types to be matched against.
Returns
- bool
trueif the specifiedtypecontains one or more of the target types specified throughout this member's inheritance chain; otherwise,false.
Exceptions
- ArgumentNullException
typecannot be null -or-interfaceTypescannot be null.
HasKeyValuePairImplementation(Type)
Determines whether the specified type implements either DictionaryEntry or KeyValuePair<TKey, TValue>.
public static bool HasKeyValuePairImplementation(this Type type)
Parameters
Returns
- bool
trueif the specifiedtypeimplements either DictionaryEntry or KeyValuePair<TKey, TValue>.; otherwise,false.
Exceptions
- ArgumentNullException
typecannot be null.
HasTypes(Type, params Type[])
Determines whether the specified type type contains one or more of the specified target types.
public static bool HasTypes(this Type type, params Type[] targets)
Parameters
Returns
- bool
trueif thetypecontains one or more of the specified target types; otherwise,false.
Exceptions
- ArgumentNullException
typecannot be null -or-targetscannot be null.
IsComplex(Type)
Determines whether the specified type is a complex Type.
public static bool IsComplex(this Type type)
Parameters
Returns
Exceptions
- ArgumentNullException
typeis null.
IsNullable(Type)
Determines whether the specified type is a nullable ValueType.
public static bool IsNullable(this Type type)
Parameters
Returns
- bool
trueif the specifiedtypeis nullable; otherwise,false.
Exceptions
- ArgumentNullException
typecannot be null.
IsSimple(Type)
Determines whether the specified type is a simple Type.
public static bool IsSimple(this Type type)
Parameters
Returns
Exceptions
- ArgumentNullException
typeis null.
ToFriendlyName(Type, Action<TypeNameOptions>)
Converts the name of the type with the intend to be understood by humans.
public static string ToFriendlyName(this Type type, Action<TypeNameOptions> setup = null)
Parameters
typeTypeThe Type to extend.
setupAction<TypeNameOptions>The TypeNameOptions which may be configured.
Returns
Exceptions
- ArgumentNullException
typecannot be null.
ToTypeCode(Type)
Gets the underlying type code of the specified type.
public static TypeCode ToTypeCode(this Type type)
Parameters
Returns
- TypeCode
The code of the underlying type, or Empty if
typeis null.