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

  • Home
  • SEARCH
  • 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 8234749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:35:20+00:00 2026-06-07T18:35:20+00:00

How can I create a closure that uses a reflected type argument? Targeting .net

  • 0

How can I create a closure that uses a reflected type argument? Targeting .net 3.5

Without reflection I would have

void Main()
{
    int i = 0;
    Action<Foo> doSomething = (foo) => i += foo.GetNumber();
    var myFoo = new Foo();
    myFoo.UseFoo(doSomething);
    Console.WriteLine(i);
}

class Foo
{
    public int GetNumber() { return 4; }
    public void UseFoo(Action<Foo> doSomething)
    {
        doSomething(this);
    }
}

When Foo is a type obtained via reflection from another assembly, how would I set doSomething?

void Main()
{
    Type fooType = GetType("Foo");
    int i = 0;
    object doSomething = // ???;

    var myFoo = Activator.CreateInstance(fooType);
    fooType.GetMethod("UseFoo").Invoke(myFoo, new object[] { doSomething });
    Console.WriteLine(i);
}
  • 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-06-07T18:35:22+00:00Added an answer on June 7, 2026 at 6:35 pm

    I used both DocXcz and Nikola Anusev’s answers to get to this

    static void Main()
    {
        var fooType = typeof(Foo); // get type via any method
        int i = 0;
    
        Action<object> doSomething = (foo) => i += (int)foo.GetType().GetMethod("GetNumber").Invoke(foo, null);
        var typedDoSomething = (typeof(Program)).GetMethod("DelegateHelper").MakeGenericMethod(fooType).Invoke(null, new object[] { doSomething });
    
        var myFoo = Activator.CreateInstance(fooType);
        fooType.GetMethod("UseFoo").Invoke(myFoo, new object[] { typedDoSomething });
        Console.WriteLine(i);
    }
    
    public static Action<T> DelegateHelper<T>(Action<object> generic)
    {
        return x => generic(x);
    }
    

    Basically I needed to use a generic helper method to do the conversion of the Action<object> to an Action<T> determined at runtime

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

Sidebar

Related Questions

I have manage to create a small size js application that uses jQuery and
I can use immediate invocation to create an object with methods that have access
I can create a contact that is not mail enabled, but how do I
One can create an anonymous object that is initialized through constructor parameters, such as
I am attempting to create a jQuery plugin for my own use that can
I am trying to create a dynamic accordion. My problem is that I can
I currently have a closure table used for hierarchical data that has 5 million
If I have a folder such as: code/try_closure code/try_http_send [...] I can create a
I'm trying to create a function on the fly that would return one constant
The code below uses Javascript to create a base class, eventRaiser , that has

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.