Class TypeExtensions
- Namespace
- Cuemon.Extensions.Reflection
- Assembly
- Cuemon.Extensions.Reflection.dll
Extension methods for the Type class.
public static class TypeExtensions
- Inheritance
-
TypeExtensions
Examples
TypeExtensions in the Reflection namespace provides methods for inspecting type hierarchies and enumerating members across the inheritance tree. This example uses typeof(Stream) to retrieve derived, inherited, and hierarchy types via GetDerivedTypes, GetInheritedTypes, and GetHierarchyTypes, then uses TypeCatalog (extending BaseCatalog) with GetAllProperties, GetAllEvents, GetAllFields, and GetAllMethods to enumerate members. It also demonstrates GetRuntimePropertiesExceptOf<BaseCatalog> to exclude inherited properties, GetEmbeddedResources for assembly resource lookup, and ToFullNameIncludingAssemblyName for a fully qualified type name. Console output confirms member names, hierarchy inclusion (e.g., Stream in GetHierarchyTypes), and assembly-qualified type identity.
using System;
using System.IO;
using System.Linq;
using Cuemon;
using Cuemon.Extensions.Reflection;
using Cuemon.Reflection;
namespace MyApp.Examples;
public static class TypeExtensionsExample
{
public static void Demonstrate()
{
var streamType = typeof(Stream);
var derivedTypes = streamType.GetDerivedTypes().Where(type => type.IsPublic).Select(type => type.Name).Take(3).ToArray();
var inheritedTypes = streamType.GetInheritedTypes().Select(type => type.Name).ToArray();
var properties = typeof(TypeArgumentOutOfRangeException).GetAllProperties().Select(property => property.Name).ToArray();
var events = typeof(TypeCatalog).GetAllEvents().Select(@event => @event.Name).ToArray();
var fields = typeof(TypeCatalog).GetAllFields().Select(field => field.Name).ToArray();
var methods = typeof(TypeCatalog).GetAllMethods().Select(method => method.Name).ToArray();
var hierarchy = typeof(Stream).GetHierarchyTypes().Select(type => type.Name).ToArray();
var resources = typeof(TypeExtensionsExample).GetEmbeddedResources("missing", ManifestResourceMatch.ContainsName);
var ownProperties = typeof(TypeCatalog).GetRuntimePropertiesExceptOf<BaseCatalog>().Select(property => property.Name).ToArray();
var fullName = typeof(TypeCatalog).ToFullNameIncludingAssemblyName();
Console.WriteLine(string.Join(", ", derivedTypes));
Console.WriteLine(string.Join(", ", inheritedTypes));
Console.WriteLine(properties.Contains(nameof(TypeArgumentOutOfRangeException.ActualValue)));
Console.WriteLine(events.Contains(nameof(TypeCatalog.Changed)));
Console.WriteLine(fields.Contains(nameof(TypeCatalog._state)));
Console.WriteLine(methods.Contains(nameof(TypeCatalog.MarkChanged)));
Console.WriteLine(hierarchy.Contains(nameof(Stream)));
Console.WriteLine(resources.Count);
Console.WriteLine(ownProperties.Contains(nameof(TypeCatalog.Name)));
Console.WriteLine(fullName.Contains(nameof(TypeCatalog)));
}
private abstract class BaseCatalog
{
public int Id { get; set; }
}
private sealed class TypeCatalog : BaseCatalog
{
internal string _state = "draft";
public string Name { get; set; } = "Extensions";
public event EventHandler Changed;
public void MarkChanged()
{
Changed?.Invoke(this, EventArgs.Empty);
}
}
}
Methods
GetAllEvents(Type, Action<MemberReflectionOptions>)
Retrieves a collection that represents all events defined on the specified source and its inheritance chain.
public static IEnumerable<EventInfo> GetAllEvents(this Type source, Action<MemberReflectionOptions> setup = null)
Parameters
sourceTypeThe Type to extend.
setupAction<MemberReflectionOptions>The MemberReflectionOptions which may be configured.
Returns
- IEnumerable<EventInfo>
An IEnumerable<T> that contains all EventInfo objects of the specified
sourceand its inheritance chain.
GetAllFields(Type, Action<MemberReflectionOptions>)
Retrieves a collection that represents all fields defined on the specified source and its inheritance chain.
public static IEnumerable<FieldInfo> GetAllFields(this Type source, Action<MemberReflectionOptions> setup = null)
Parameters
sourceTypeThe Type to extend.
setupAction<MemberReflectionOptions>The MemberReflectionOptions which may be configured.
Returns
- IEnumerable<FieldInfo>
An IEnumerable<T> that contains all FieldInfo objects of the specified
sourceand its inheritance chain.
GetAllMethods(Type, Action<MemberReflectionOptions>)
Retrieves a collection that represents all methods defined on the specified source and its inheritance chain.
public static IEnumerable<MethodInfo> GetAllMethods(this Type source, Action<MemberReflectionOptions> setup = null)
Parameters
sourceTypeThe Type to extend.
setupAction<MemberReflectionOptions>The MemberReflectionOptions which may be configured.
Returns
- IEnumerable<MethodInfo>
An IEnumerable<T> that contains all MethodInfo objects of the specified
sourceand its inheritance chain.
GetAllProperties(Type, Action<MemberReflectionOptions>)
Retrieves a collection that represents all properties defined on the specified source and its inheritance chain.
public static IEnumerable<PropertyInfo> GetAllProperties(this Type source, Action<MemberReflectionOptions> setup = null)
Parameters
sourceTypeThe Type to extend.
setupAction<MemberReflectionOptions>The MemberReflectionOptions which may be configured.
Returns
- IEnumerable<PropertyInfo>
An IEnumerable<T> that contains all PropertyInfo objects of the specified
sourceand its inheritance chain.
GetDerivedTypes(Type, params Assembly[])
Gets a collection (self-to-derived) of derived / descendant types of the source.
public static IEnumerable<Type> GetDerivedTypes(this Type source, params Assembly[] assemblies)
Parameters
sourceTypeThe Type to extend.
assembliesAssembly[]The assemblies to include in the search of derived types.
Returns
- IEnumerable<Type>
An IEnumerable<T> that contains the derived types of the
source.
Exceptions
- ArgumentNullException
sourcecannot be null.
GetEmbeddedResources(Type, string, ManifestResourceMatch)
Loads the embedded resources from the associated Assembly of the specified Type following the ManifestResourceMatch ruleset of match.
public static IDictionary<string, Stream> GetEmbeddedResources(this Type source, string name, ManifestResourceMatch match)
Parameters
sourceTypeThe source type to load the resource from.
namestringThe name of the resource being requested.
matchManifestResourceMatchThe match ruleset to apply.
Returns
- IDictionary<string, Stream>
A Stream representing the loaded resources; null if no resources were specified during compilation, or if the resource is not visible to the caller.
Exceptions
- ArgumentNullException
sourcecannot be null -or-namecannot be null.- ArgumentException
namecannot be empty or consist only of white-space characters.- InvalidEnumArgumentException
matchwas not in the range of valid values.
GetHierarchyTypes(Type, params Assembly[])
Gets a collection (inherited-to-self-to-derived) of inherited / ancestor and derived / descendant types of the source.
public static IEnumerable<Type> GetHierarchyTypes(this Type source, params Assembly[] assemblies)
Parameters
sourceTypeThe Type to extend.
assembliesAssembly[]The assemblies to include in the search of derived types.
Returns
- IEnumerable<Type>
An IEnumerable<T> that contains a sorted (base-to-derived) collection of inherited and derived types of the
source.
Exceptions
- ArgumentNullException
sourcecannot be null.
GetInheritedTypes(Type)
Gets a collection (inherited-to-self) of inherited / ancestor types of the source.
public static IEnumerable<Type> GetInheritedTypes(this Type source)
Parameters
Returns
- IEnumerable<Type>
An IEnumerable<T> that contains the inherited types of the
source.
Exceptions
- ArgumentNullException
sourcecannot be null.
GetRuntimePropertiesExceptOf<T>(Type)
Retrieves a collection that represents all the properties defined on a specified type except those defined on T.
public static IEnumerable<PropertyInfo> GetRuntimePropertiesExceptOf<T>(this Type type)
Parameters
typeTypeThe type that contains the properties to include except those defined on
T.
Returns
- IEnumerable<PropertyInfo>
A collection of properties for the specified
typeexcept those defined onT.
Type Parameters
TThe type to exclude properties on
type.
Exceptions
- ArgumentNullException
typecannot be null.
ToFullNameIncludingAssemblyName(Type)
Converts the Type to its equivalent string representation.
public static string ToFullNameIncludingAssemblyName(this Type type)
Parameters
Returns
- string
A string that contains the fully qualified name of the type, including its namespace, comma delimited with the simple name of the assembly.