Table of Contents

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

type Type

The Type to extend.

result Type

When this method returns, contains the Type of the generic parameter TMarker of IDependencyInjectionMarker<TMarker>; otherwise null if the specified type does not have an implemenation of the IDependencyInjectionMarker<TMarker> interface.

Returns

bool

true if the specified type contains an IDependencyInjectionMarker<TMarker> interface, false otherwise.