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 242997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:53:04+00:00 2026-05-11T20:53:04+00:00

I want to emit a method that returns a Func<>. Inside this method I

  • 0

I want to emit a method that returns a Func<>. Inside this method I have to create a delegate or a lambda expression which exactly serves the return type.

Altogether it should look like this:

// I have a resolve method that will be called inside my missing method
// This is it's signature:
object Resolve( params object[] args);

// This is how I use it:
var barFactory = ( Func<IBar> )MissingMethod( typeof( IBar ) );
var bar = barFactory.Invoke();

// or - with one string argument:
var fooFactory = ( Func<string, IFoo> )MissingMethod( typeof( IFoo ), typeof( string ) );
var foo = fooFactory.Invoke( "argument for foo" );

Inside the MissingMethod() it should look like:

object MissingMethod( Type returnType, params Type[] argTypes )
{
  // Create the type of Func<> based on the passed returnType and the argTypes
  var funcType = typeof(Func<,...,>).MakeGenericType( ... )

  // Here I have to use the Resolve() method and cast the lambda to the correct type
  return (cast to funcType)( (arg1, arg2) => Resolve( arg1, arg2 ) );
}

I think that the only way to get my MissingMethod() is, to use reflection.emit.

Do you know good resources or tutorials about emitting a lambda or a delegate?

Do you see another possible solution for this problem?

EDIT:
Here is a scenario of what I want to achive:

static void Main()
{
  var container = new Container();
  container.Register<Foo>();
  container.Register<ConsumerClass>();

  var consumerClass = Container.Resolve<ConsumerClass>();
}

class Foo()
{
  public Foo( string argument ) {}
}

class ConsumerClass
{
  public ConsumerClass( [Inject] Func<string, Foo> factory )
  {
    var foo1 = factory.Invoke( "first foo" );
    var foo2 = factory.Invoke( "another foo" );
    // ...
  }
}

I am trying to implement the Container and the Resolve() method. I know that there is a “Foo” type registered. And I know that its constructor needs a string to be invoked.

When I have to resolve the type “ConsumerClass” I see that it wants to get a Func injected. That is not exactly what my container can provide, because normally it provides single instaces to Foo like this:

Container.Resolve<Foo>( "argument" );

But nevertheless the container should be able to provide a Func, too. It has all informations needed.

But now I am stuck in creating this bound Func<,>. And remeber it could be a Func<,,,>, too. So I am looking for a solution that can create my this delegates on the fly. They have to be castable to the exact bound type.

EDIT:
I am not sure how to describe it better … I am trying to do something like this. But I do not want to pass a target. Instead of

delegate void object LateBoundMethod( object target, object[] arguments );

my delegate should look like

delegate void object LateBoundMethod( object[] arguments );

and the target is provided as an instance field. By taking and ‘improving’ the solution of Marc I get:

private Delegate CreateDelegate( Type returnType, Type[] parameterTypes )
{
  m_Type = returnType;

  var i = 0;
  var param = Array.ConvertAll( parameterTypes, arg => Expression.Parameter( arg, "arg" + i++ ) );
  var asObj = Array.ConvertAll( param, p => Expression.Convert( p, typeof( object ) ) );
  var argsArray = Expression.NewArrayInit( typeof( object ), asObj );

  var callEx = Expression.Call( null, typeof( FuncFactory ).GetMethod( "Resolve" ), argsArray );
  var body = Expression.Convert( callEx, returnType );

  var ret = Expression.Lambda( body, param ).Compile();
  return ret;
}

private readonly Container m_Container;
private Type m_Type;

public object Resolve( params object[] args )
{
  return m_Container.Resolve( m_Type, args );
}

But this is incomplete. The Resolve()-method is not static anymore (because it needs two instance fields) and cannot be called. So the problem here is

var callEx = Expression.Call( null, typeof( FuncFactory ).GetMethod( "Resolve" ), argsArray );

Instead of passing null as the first argument I think I need a reference to ‘this’. How do I do that?

  • 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-11T20:53:04+00:00Added an answer on May 11, 2026 at 8:53 pm

    The first problem is that Func<...> doesn’t exist – you’d need to code to Func<>, Func<,>, Func<,,>, Func<,,,> separately.

    Now; I understand the code, but I’m not sure what the scenario is that you are trying to solve… can you clarify? There are probably better options…

    If it is as complex as it sounds, a custom Expression is probably the most appropriate option (far simpler than Reflection.Emit).


    This works, for example…

    static Delegate MissingFunc(Type result, params Type[] args)
    {
        int i = 0;
        var param = Array.ConvertAll(args,
            arg => Expression.Parameter(arg, "arg" + i++));
        var asObj = Array.ConvertAll(param,
            p => Expression.Convert(p, typeof(object)));
        var argsArray = Expression.NewArrayInit(typeof(object), asObj);
        var body = Expression.Convert(Expression.Call(
                    null, typeof(Program).GetMethod("Resolve"),
                    argsArray), result);
        return Expression.Lambda(body, param).Compile();
    }
    static void Main()
    {
        var func2 = MissingFunc(typeof(string), typeof(int), typeof(float));
    }
    public static object Resolve( params object[] args) {
        throw new NotImplementedException();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 220k
  • Answers 220k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer function(reinterpret_cast<D3DXVECTOR3*>(&vTest)); Generally speaking you should avoid reinterpret_cast though. May 12, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer tableCell.Controls.Add(DropDownListOraInizio); tableCell.Controls.Add(new LiteralControl(":")); tableCell.Controls.Add(DropDownListMinutoInizio); May 12, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer Try the value property, like this: var fu1 = document.getElementById("FileUpload1");… May 12, 2026 at 11:55 pm

Related Questions

I want to emit a method that returns a Func<>. Inside this method I
How do I define a DynamicMethod for a delegate that has an out -parameter,
I'm writing a C++ class to read input from a file into preallocated buffers
I need to encode/decode UTF-16 byte arrays to and from java.lang.String . The byte
I am writing a (very small) framework for checking pre- and postconditions of methods.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.