OutWit.Common.Reflection 1.1.0

dotnet add package OutWit.Common.Reflection --version 1.1.0
                    
NuGet\Install-Package OutWit.Common.Reflection -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OutWit.Common.Reflection" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OutWit.Common.Reflection" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="OutWit.Common.Reflection" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OutWit.Common.Reflection --version 1.1.0
                    
#r "nuget: OutWit.Common.Reflection, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OutWit.Common.Reflection@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OutWit.Common.Reflection&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=OutWit.Common.Reflection&version=1.1.0
                    
Install as a Cake Tool

OutWit.Common.Reflection

EventUtils: Universal Event Handling Made Easy

The EventUtils class provides utilities for working with .NET events, enabling developers to create universal handlers for any event in a highly reusable and reflective manner. This is particularly useful in scenarios requiring dynamic event handling or generalized solutions for complex event-driven architectures.

Features

1. Retrieve All Events of a Type

The GetAllEvents method recursively retrieves all events from a given type, including:

  • Events defined on the type itself
  • Events defined in its base types
  • Events from implemented interfaces
Type type = typeof(SomeClass);
IEnumerable<EventInfo> allEvents = type.GetAllEvents();
foreach (var eventInfo in allEvents)
{
    Console.WriteLine(eventInfo.Name);
}

2. Create Universal Event Handlers

The CreateUniversalHandler method allows you to create a delegate for any event using a universal handler. This handler acts as a single entry point for all events of a type, dynamically handling event calls.

Key Benefits:
  • Dynamically binds to event handlers at runtime.
  • Enables centralized logging, debugging, or analytics for all events.
  • Simplifies handling events with unknown signatures at compile-time.
Example Usage
using OutWit.Common.Reflection;

public class Example
{
    public static void Main()
    {
        var obj = new SomeClass();

        EventInfo eventInfo = typeof(SomeClass).GetEvent("SomeEvent")!;

        var handler = eventInfo.CreateUniversalHandler(
            obj,
            (sender, eventName, parameters) =>
            {
                Console.WriteLine($"Event {eventName} triggered on {sender} with parameters: {string.Join(", ", parameters)}");
            });

        eventInfo.AddEventHandler(obj, handler);

        obj.TriggerSomeEvent(); // Triggers the universal handler
    }
}

public class SomeClass
{
    public event EventHandler? SomeEvent;

    public void TriggerSomeEvent()
    {
        SomeEvent?.Invoke(this, EventArgs.Empty);
    }
}

Method Details

GetAllEvents
public static IEnumerable<EventInfo> GetAllEvents(this Type type)
  • Description: Recursively retrieves all events from a type, including those from base types and interfaces.
  • Returns: A collection of EventInfo objects.
CreateUniversalHandler
public static Delegate CreateUniversalHandler<TSender>(
    this EventInfo me,
    TSender sender,
    UniversalEventHandler<TSender> handler
) where TSender : class;
  • Description: Creates a dynamic delegate that binds a universal handler to an event.
  • Parameters:
    • me: The EventInfo describing the event.
    • sender: The object raising the event.
    • handler: The universal event handler delegate.
  • Returns: A Delegate matching the event's signature.
UniversalEventHandler

A delegate used for the universal handler:

public delegate void UniversalEventHandler<in TSender>(
    TSender sender,
    string eventName,
    object[] parameters
) where TSender : class;
  • Parameters:
    • sender: The object that raised the event.
    • eventName: The name of the event.
    • parameters: The event arguments passed during invocation.

Installation

Include the OutWit.Common.Reflection namespace in your project to access EventUtils.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on OutWit.Common.Reflection:

Package Downloads
OutWit.Communication

The core communication library of the WitRPC framework, providing base RPC functionality such as messaging, dynamic proxy support, and extensibility for multiple transports, serialization formats, and encryption.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 270 6/29/2025
1.0.1 856 1/11/2025
1.0.0 278 1/2/2025