TypeExtensions Class
Definition
- Namespace
- Cuemon.Extensions.Reflection
- Assemblies
- 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
| Name | Description |
|---|---|
| GetAllEvents(Type, Action<MemberReflectionOptions>) | Retrieves a collection that represents all events defined on the specified |
| GetAllFields(Type, Action<MemberReflectionOptions>) | Retrieves a collection that represents all fields defined on the specified |
| GetAllMethods(Type, Action<MemberReflectionOptions>) | Retrieves a collection that represents all methods defined on the specified |
| GetAllProperties(Type, Action<MemberReflectionOptions>) | Retrieves a collection that represents all properties defined on the specified |
| GetDerivedTypes(Type, params Assembly[]) | Gets a collection (self-to-derived) of derived / descendant types of the |
| GetEmbeddedResources(Type, string, ManifestResourceMatch) | Loads the embedded resources from the associated Assembly of the specified Type following the ManifestResourceMatch ruleset of |
| GetHierarchyTypes(Type, params Assembly[]) | Gets a collection (inherited-to-self-to-derived) of inherited / ancestor and derived / descendant types of the |
| GetInheritedTypes(Type) | Gets a collection (inherited-to-self) of inherited / ancestor types of the |
| GetRuntimePropertiesExceptOf<T>(Type) | Retrieves a collection that represents all the properties defined on a specified |
| ToFullNameIncludingAssemblyName(Type) | Converts the Type to its equivalent string representation. |