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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:53:17+00:00 2026-05-31T03:53:17+00:00

Ideally I want to be using the CreateObject delegate because the code generating these

  • 0

Ideally I want to be using the CreateObject delegate because the code generating these dynamic methods is for a deserializer that should be able to handle any type (at least, primitives, structs, & class instances). However, I ran into a problem with the CreateObject delegate type so I decided to try the CreateRectangle delegate to debug things. I got a little closer to a working solution but something else isn’t right. What’s wrong with my code for both cases? That is, how can I make the dynamic method work for both CreateObject and CreateRectangle? Or, is my calling code the culprit?

Output:

{X=0,Y=0,Width=0,Height=0}

Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.

Exception has been thrown by the target of an invocation.
    Common Language Runtime detected an invalid program.

Code:

using System;
using System.Drawing;
using System.Reflection.Emit;

namespace Experiments {
    public class Program {
        private delegate object CreateObject();
        private delegate Rectangle CreateRectangle();

        static void Main() {
            Console.WriteLine(new Rectangle());
            Console.WriteLine();
            var dm = BuildDynamicMethod();
            TryCreateDelegate<CreateObject>(dm);
            Console.WriteLine();
            TryCreateDelegate<CreateRectangle>(dm);
            Console.WriteLine();
            Console.ReadKey();
        }

        private static void TryCreateDelegate<T>(DynamicMethod dm) {
            try {
                var co = dm.CreateDelegate(typeof (T));
                var value = co.DynamicInvoke(null);
                Console.WriteLine(value);
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
                var indent = 0;
                while (ex.InnerException != null) {
                    indent++;
                    ex = ex.InnerException;
                    Console.WriteLine(new string('\t', indent) + ex.Message);
                }
            }
        }

        private static DynamicMethod BuildDynamicMethod() {
            var tr = typeof(Rectangle);
            var dm = new DynamicMethod("buildNewRectangle", tr, Type.EmptyTypes);
            var il = dm.GetILGenerator();
            il.Emit(OpCodes.Ldloca_S, (byte)0);
            il.Emit(OpCodes.Initobj, tr);
            il.Emit(OpCodes.Ldloc_0);
            il.Emit(OpCodes.Box, tr);
            il.Emit(OpCodes.Ret);
            return dm;
        }
    }
}
  • 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-31T03:53:18+00:00Added an answer on May 31, 2026 at 3:53 am
    private delegate object CreateObject();
    private delegate Rectangle CreateRectangle();
    
    static void Main()
    {
      Console.WriteLine(new Rectangle());
      Console.WriteLine();
      TryCreateDelegate<CreateObject>(BuildDynamicMethod_Boxed());
      Console.WriteLine();
      TryCreateDelegate<CreateRectangle>(BuildDynamicMethod());
      Console.WriteLine();
    }
    
    private static DynamicMethod BuildDynamicMethod_Boxed()
    {
      var TRect = typeof(Rectangle);
      var dm = new DynamicMethod("buildNewRectangle", typeof(object), Type.EmptyTypes);
      var il = dm.GetILGenerator();
      il.Emit(OpCodes.Ldloca_S, il.DeclareLocal(TRect));
      il.Emit(OpCodes.Initobj, TRect);
      il.Emit(OpCodes.Ldloc_0);      
      il.Emit(OpCodes.Box, TRect);
      il.Emit(OpCodes.Ret);
      return dm;
    }
    
    private static DynamicMethod BuildDynamicMethod()
    {
      var TRect = typeof(Rectangle);
      var dm = new DynamicMethod("buildNewRectangle", TRect, Type.EmptyTypes);
      var il = dm.GetILGenerator();
      il.Emit(OpCodes.Ldloca_S, il.DeclareLocal(TRect));
      il.Emit(OpCodes.Initobj, TRect);
      il.Emit(OpCodes.Ldloc_0);
      il.Emit(OpCodes.Ret);
      return dm;
    }
    
    private static void TryCreateDelegate<T>(DynamicMethod dm)
    {
      try
      {
        var co = dm.CreateDelegate(typeof(T));
        var value = co.DynamicInvoke();
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
        var indent = 0;
        while (ex.InnerException != null)
        {
          indent++;
          ex = ex.InnerException;
          Console.WriteLine(new string('\t', indent) + ex.Message);
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whats the best way to zip up files using C#? Ideally I want to
I want to make an FTP connection (ideally using Coldfusion 8, but Java is
I have a situation where ideally I want to be able to log-in to
I want to be able to show/hide the rows in a table using jquery.
im having a problem when downloading a csv file using php, ideally i want
Ideally I want a function something like the following (this example doesn't compile): void
We have several common libs. Ideally we want them all to use the latest
Are there any good tools to make css sprites? IDEALLY I'd want to give
I want all my applications log to be centralized (ideally in near real-time). We
I want to inject a connection string into my repository but ideally, I want

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.