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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:10:26+00:00 2026-05-26T06:10:26+00:00

This is best explained using code. I have a generic class that has a

  • 0

This is best explained using code. I have a generic class that has a method that returns an integer. Here is a simple version for the purposes of explaining…

public class Gen<T>
{
    public int DoSomething(T instance)
    {
        // Real code does something more interesting!
        return 1;
    }
}

At runtime I use reflection to discover the type of something and then want to create an instance of my Gen class for that specific type. That is easy enough and done like this…

Type fieldType = // This is the type I have discovered
Type genericType = typeof(Gen<>).MakeGenericType(fieldType);
object genericInstance = Activator.CreateInstance(genericType);

I now want to create an Expression that will take as a parameter an instance of the generic type and then calls the DoSomething method of that type. So I want the Expression to effectively perform this…

int answer = genericInstance.DoSomething(instance);

…except I do not have the ‘instance’ until some point later at runtime and the genericInstance is the generated type as can be seen above. My attempt at creating the Lambda for this is as follows…

MethodInfo mi = genericType.GetMethod("DoSomething", 
                                      BindingFlags.Instance | BindingFlags.Public);

var p1 = Expression.Parameter(genericType, "generic");
var p2 = Expression.Parameter(fieldType, "instance");

var x = Expression.Lambda<Func<genericType, fieldType, int>>
            (Expression.Call(p1, mi, p2), 
             new[] { p1, p2 }).Compile();

…so that later on I can call it with something like this…

int answer = x(genericInstance, instance);

Of course, you cannot provide Func with instance parameters and so I have no idea how to parameterize the Lambda generation. Any ideas?

  • 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-26T06:10:27+00:00Added an answer on May 26, 2026 at 6:10 am

    I think you would just use the Expression.Lambda that takes the delegate type as a type rather then as a generic, and create your Func on the fly like you are with Gen<>:

    MethodInfo mi = genericType.GetMethod("DoSomething",
                                    BindingFlags.Instance | BindingFlags.Public);
    
    var p1 = Expression.Parameter(genericType, "generic");
    var p2 = Expression.Parameter(fieldType, "instance");
    var func = typeof (Func<,,>);
    var genericFunc = func.MakeGenericType(genericType, fieldType, typeof(int));
    var x = Expression.Lambda(genericFunc, Expression.Call(p1, mi, p2),
                    new[] { p1, p2 }).Compile();
    

    This will return a Delegate rather than a strongly typed Func, but you can of course cast it if needed (and seemingly difficult if you don’t know what you are casting to), or dynamically invoke it using DynamicInvoke on it.

    int answer = (int) x.DynamicInvoke(genericInstance, instance);
    

    EDIT:

    A good idea that does indeed work. Unfortunately the reason I want to use a strongly typed compiled Lambda is performance. Using DynamicInvoke is prettty slow compared to a typed Lambda.

    This seems to work without the need of a dynamic invoke.

    var p1 = Expression.Parameter(genericType, "generic");
    var p2 = Expression.Parameter(fieldType, "instance");
    var func = typeof(Func<,,>);
    var genericFunc = func.MakeGenericType(genericType, fieldType, typeof(int));
    var x = Expression.Lambda(genericFunc, Expression.Call(p1, mi, p2), new[] { p1, p2 });
    var invoke = Expression.Invoke(x, Expression.Constant(genericInstance), Expression.Constant(instance));
    var answer = Expression.Lambda<Func<int>>(invoke).Compile()();
    

    EDIT 2:

    A greatly simplified version:

    Type fieldType = ;// This is the type I have discovered
    Type genericType = typeof(Gen<>).MakeGenericType(fieldType);
    object genericInstance = Activator.CreateInstance(genericType);
    MethodInfo mi = genericType.GetMethod("DoSomething",
                                    BindingFlags.Instance | BindingFlags.Public);
    var value = Expression.Constant(instance, fieldType);
    var lambda = Expression.Lambda<Func<int>>(Expression.Call(Expression.Constant(genericInstance), mi, value));
    var answer = lambda.Compile()();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Best explained with code I think, this is just a simple example: public class
I have a simple class that handles the connection being made between a client
Im not sure if i have explaned this the best i can but, here
I want to explain this as best as I can. I have a Webgrid
I'll try to explain this problem the best way i can with code: double
Alright, I'll try to explain this the best I can. I have an iPhone
Ok this is hard to explain, but here goes. I have a 3D list
index.php has this jquery code which loads notifications.inc.php into a div on the page
Best explained with code: long pieceLength = Math.Pow(2,18); //simplification ... public void HashFile(string path)
Will do my best to explain this. => I am using Fancybox and everything

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.