Class TypeExtensions
- Namespace
- Cuemon.Extensions.DependencyInjection
- Assembly
- Cuemon.Extensions.DependencyInjection.dll
Extension methods for the Type class.
public static class TypeExtensions
- Inheritance
-
TypeExtensions
Examples
The following example asks TryGetDependencyInjectionMarker whether a generic service type carries an IDependencyInjectionMarker<TMarker> contract and then reads the discovered marker type. It tests a DefaultService<OrdersChannel> that implements the marker interface, and a plain string type that does not. The boolean results and the resolved marker type are written to the console, demonstrating how to detect marker-interface contracts for DI service wiring at runtime.
using System;
using Cuemon.Extensions.DependencyInjection;
namespace Cuemon.Docs.Samples.DependencyInjection
{
public static class TypeExtensionsExample
{
public static void Demonstrate()
{
var marked = typeof(DefaultService<OrdersChannel>).TryGetDependencyInjectionMarker(out var markerType);
var plain = typeof(string).TryGetDependencyInjectionMarker(out _);
Console.WriteLine(marked);
Console.WriteLine(markerType == typeof(OrdersChannel));
Console.WriteLine(plain);
}
public sealed class OrdersChannel
{
}
public interface IMessageService
{
}
public interface IMessageService<TMarker> : IMessageService, IDependencyInjectionMarker<TMarker>
{
}
public sealed class DefaultService<TMarker> : IMessageService<TMarker>
{
}
}
}
Methods
TryGetDependencyInjectionMarker(Type, out Type)
Returns a value that indicates whether the specified type contains an IDependencyInjectionMarker<TMarker> interface.
public static bool TryGetDependencyInjectionMarker(this Type type, out Type result)
Parameters
typeTypeThe Type to extend.
resultTypeWhen this method returns, contains the Type of the generic parameter
TMarkerof IDependencyInjectionMarker<TMarker>; otherwisenullif the specifiedtypedoes not have an implemenation of the IDependencyInjectionMarker<TMarker> interface.
Returns
- bool
trueif the specifiedtypecontains an IDependencyInjectionMarker<TMarker> interface,falseotherwise.