![]() |
VOOZH | about |
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" />Directory.Packages.props
<PackageReference Include="OutWit.Common.Reflection" />Project file
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.1Install as a Cake Addin
#tool nuget:?package=OutWit.Common.Reflection&version=1.2.1Install as a Cake Tool
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.
The GetAllEvents method recursively retrieves all events from a given type, including:
Type type = typeof(SomeClass);
IEnumerable<EventInfo> allEvents = type.GetAllEvents();
foreach (var eventInfo in allEvents)
{
Console.WriteLine(eventInfo.Name);
}
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.
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);
}
}
GetAllEventspublic static IEnumerable<EventInfo> GetAllEvents(this Type type)
EventInfo objects.CreateUniversalHandlerpublic static Delegate CreateUniversalHandler<TSender>(
this EventInfo me,
TSender sender,
UniversalEventHandler<TSender> handler
) where TSender : class;
me: The EventInfo describing the event.sender: The object raising the event.handler: The universal event handler delegate.Delegate matching the event's signature.UniversalEventHandlerA delegate used for the universal handler:
public delegate void UniversalEventHandler<in TSender>(
TSender sender,
string eventName,
object[] parameters
) where TSender : class;
sender: The object that raised the event.eventName: The name of the event.parameters: The event arguments passed during invocation.Include the OutWit.Common.Reflection namespace in your project to access EventUtils.
Licensed under the Apache License, Version 2.0. See LICENSE.
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/)".
"OutWit" and the OutWit logo are used to identify the official project by Dmitry Ratner.
You may:
You may not:
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 is compatible. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
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. |
This package is not used by any popular GitHub repositories.