Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6765265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:41:31+00:00 2026-05-26T14:41:31+00:00

I want to register a generic delegate that resolves itself at runtime, but I

  • 0

I want to register a generic delegate that resolves itself at runtime, but I cannot find a way to do this on generics.

Given a delegate that looks like this:

public delegate TOutput Pipe<in TInput, out TOutput>(TInput input);

And given a discretely registered delegate that look like this:

public class AnonymousPipe<TInput, TOutput>
{
   public Pipe<TInput, TOutput> GetPipe(IContext context)
   {...}

I want to register a function along the lines of this:

builder.RegisterGeneric(typeof(Pipe<,>)).As(ctx => 
{
   var typeArray = ctx.RequestedType.GetGenericArguments();
   // this can be memoized 
   var pipeDefinition = ctx.Resolve(typeof(AnonymousPipe<,>).MakeGenericType(typeArray));

   return pipeDefinition.GetPipe(ctx);

I cannot find a way to provide an implementation of the generic as a parameter in Autofac – I may just be missing something. I know I can do this through a generic object or interface, but I want to stick with the lightness of a delegate. It makes unit testing super simple on the injection of these.

Any thoughts? I am having to do discrete registrations at the moment(one per type combination and no generics).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:41:32+00:00Added an answer on May 26, 2026 at 2:41 pm

    I can only come up with the registration source solution (the universal hammer in Autofac.)

    class PipeSource : IRegistrationSource
    {
        public bool IsAdapterForIndividualComponents { get { return true; } }
    
        public IEnumerable<IComponentRegistration> RegistrationsFor(
            Service service,
            Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
        {
            var swt = service as IServiceWithType;
            if (swt == null || !swt.ServiceType.IsGenericType)
                yield break;
    
            var def = swt.ServiceType.GetGenericTypeDefinition();
            if (def != typeof(Pipe<,>))
                yield break;
    
            var anonPipeService = swt.ChangeType(
                typeof(AnonymousPipe<,>).MakeGenericType(
                    swt.ServiceType.GetGenericArguments()));
    
            var getPipeMethod = anonPipeService.ServiceType.GetMethod("GetPipe");
    
            foreach (var anonPipeReg in registrationAccessor(anonPipeService))
            {
                yield return RegistrationBuilder.ForDelegate((c, p) => {
                        var anon = c.ResolveComponent(anonPipeReg, p);
                        return getPipeMethod.Invoke(anon, null); })
                    .As(service)
                    .Targeting(anonPipeReg)
                    .CreateRegistration();
            }
        }
    }
    

    Then:

    builder.RegisterSource(new PipeSource());
    

    Now, I’m certain that I can’t type that code into a web page and have it actually compile and run, but it might come close 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want my apache to register some of my predefined environment so that
I want to achieve something like this in C# 3.5: public void Register<T>() :
I've just stumbled upon this: within a Unity container, I want to register IDictionary<TK,
I want to register all my types implementing IManager so that they can be
I want to write a CommandProcessor using generics. The idea being that a Command
I have a collection of services that I want to register with Castle Windsor
Just want to register to video/photo taken events. Hopefully, but not a must, the
I want to register to get notified of all Java changes in Eclipse. I
I want to register a specific instance of an object for a type in
There is nested listviews and I want to register a button in the second

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.