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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:32:10+00:00 2026-05-13T10:32:10+00:00

I found this but tried to use it and failed. How can i create

  • 0

I found this but tried to use it and failed.

How can i create an object using reflections and make it fast by putting it in a delegate?

        DynamicMethod dm = new DynamicMethod("MyCtor", t, new Type[] { });            
        var ctor = t.GetConstructor(new Type[] { });
        ILGenerator ilgen = dm.GetILGenerator();
        ilgen.Emit(OpCodes.Ldarg_0);
        ilgen.Emit(OpCodes.Newobj, ctor);
        ilgen.Emit(OpCodes.Ret);
        var d = (Func<T>)dm.CreateDelegate(t);
        dm.Invoke(null, new object[] { });

Before putting it in a deleage i tried to at least invoke it and when i did above i get the error

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an invocation.

If i call d() instead i get the exception

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: Type must derive from Delegate.

How do i put a no param constructor into a delegate and call it?

  • 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-13T10:32:11+00:00Added an answer on May 13, 2026 at 10:32 am

    If you have access to .NET 3.5 (which your use of Func<T> suggests), you may find Expression easier than ILGenerator:

    class Foo { }
    static void Main() {
        Func<Foo> func = GetCtor<Foo>(); // cache this somewhere!
        Foo foo = func();
    }
    static Func<T> GetCtor<T>() {
        Type type = typeof(T);
        Expression body = Expression.New(type);
        return Expression.Lambda<Func<T>>(body).Compile();        
    }
    

    Pretty easy to extend that to use a specific constructor, passing arguments, or adding post-constructor property bindings; casts, conversions, etc (see this related answer). If you have a specific scenario, I’ll happily add an example.

    Note also that you should cache and re-use any such constructors – otherwise you lose the benefit (i.e. don’t recreate the delegate per-call).

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

Sidebar

Related Questions

I found this great Tablesorter plugin for jQuery but I can't make it work
I've tried all the time calculating examples I found on this site but somehow
I found this sample but its for web. Can any one check this proj.
When using clang++, how can I make CMake use the -std=c++0x flag when compiling,
I found this question but it was only similar and, more importantly, dated by
I found this similar question here , but this is really old. Was it
like in this post here by exemple I have found this blog but in
Ok, so I'm quite new to jQuery, but found this bizzare problem just now,
Found this Multimap containing pairs? , but it is not much help How would
found this little code snippet that seems to do what i want, but im

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.