OutWit.Common.Reflection
1.2.1
dotnet add package OutWit.Common.Reflection --version 1.2.1
NuGet\Install-Package OutWit.Common.Reflection -Version 1.2.1
<PackageReference Include="OutWit.Common.Reflection" Version="1.2.1" />
<PackageVersion Include="OutWit.Common.Reflection" Version="1.2.1" />
<PackageReference Include="OutWit.Common.Reflection" />
paket add OutWit.Common.Reflection --version 1.2.1
#r "nuget: OutWit.Common.Reflection, 1.2.1"
#:package OutWit.Common.Reflection@1.2.1
#addin nuget:?package=OutWit.Common.Reflection&version=1.2.1
#tool nuget:?package=OutWit.Common.Reflection&version=1.2.1
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
EventInfoobjects.
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: TheEventInfodescribing the event.sender: The object raising the event.handler: The universal event handler delegate.
- Returns: A
Delegatematching 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.
License
Licensed under the Apache License, Version 2.0. See LICENSE.
Attribution (optional)
If you use OutWit.Common.Reflection in a product, a mention is appreciated (but not required), for example: "Powered by OutWit.Common.Reflection (https://ratner.io/)".
Trademark / Project name
"OutWit" and the OutWit logo are used to identify the official project by Dmitry Ratner.
You may:
- refer to the project name in a factual way (e.g., "built with OutWit.Common.Reflection");
- use the name to indicate compatibility (e.g., "OutWit.Common.Reflection-compatible").
You may not:
- use "OutWit.Common.Reflection" as the name of a fork or a derived product in a way that implies it is the official project;
- use the OutWit.Common.Reflection logo to promote forks or derived products without permission.
| Product | Versions 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 is compatible. 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. |
-
net10.0
- No dependencies.
-
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.