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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:54:39+00:00 2026-05-18T03:54:39+00:00

I am resorting to a horrible hack in order to fill a locked-down data

  • 0

I am resorting to a horrible hack in order to fill a locked-down data type in the XNA framework: there’s an internal method in a structure that I wish to call without feeding the garbage collector.

If I keep said structure boxed in an object variable and use MethodInfo.Invoke(), that call would itself feed the garbage collector by boxing the parameters:

private object boxedTouchCollection;

void test() {
  MethodInfo addTouchLocationMethod = typeof(TouchCollection).GetMethod(
    "AddTouchLocation", BindingFlags.Instance | BindingFlags.NonPublic
  );
  addTouchLocationMethod.Invoke(
    this.boxedState, new object[] { /* parameters being boxed */ }
  );
}

I’m not sure whether Delegate.CreateDelegate() can be used here – can I just turn the first parameter into an object and it will work on the boxed structure? Or can I store my structure unboxed and declare the first parameter as ref TouchCollection?

delegate void AddTouchLocationDelegate(
  ref TouchCollection collection,
  int id,
  // ...more parameters...
);

private TouchCollection touchCollection;

void test() {
  Delegate.CreateDelegate(
    typeof(AddTouchLocationDelegate),
    typeof(ref TouchCollection), // doesn't compile
    addTouchLocationMethod
  );
}

Is there a way I can make Delegate.CreateDelegate() work?
Or will I have to resort to dynamic IL generation?

  • 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-18T03:54:39+00:00Added an answer on May 18, 2026 at 3:54 am

    Here’s one way.

    It relies on this overload of Delegate.CreateDelegate, which creates open instance-method delegates. The only tricky bit is that so you’ll have to create the appropriate delegate-type to be able to pass the struct by reference.

    I don’t think there should be any boxing with this technique – either with the arguments to the method, or with the struct itself.

    Example: (Apologies for simplifying the example-types)

    public struct Foo
    {
        // Internal method to be called. Takes a value-type parameter.
        internal void Test(int someParam)
        {
            Console.WriteLine(someParam);
        }
    
        // Custom delegate-type. Takes the Foo instance of interest 
        // by reference, as well as the argument to be passed on to Test.
        public delegate void MyDelegate(ref Foo foo, int someParam);
    
        // Creates type-safe delegate
        private static MyDelegate GetTestDelegate()
        {
            var flags = BindingFlags.Instance | BindingFlags.NonPublic;
            var methodInfo = typeof(Foo).GetMethod("Test", flags);
    
            return (MyDelegate) Delegate.CreateDelegate
                                (typeof(MyDelegate), methodInfo);       
        }
    
        static void Main()
        {
            Foo foo = new Foo();
            MyDelegate action = GetTestDelegate();
    
            // should dodge boxing
            action(ref foo, 42);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to modify existing .NET assemblies without resorting to 3rd party
Without resorting to using an ActiveX or some other kind of client side coding:
Is it possible to generate PDF Documents in an Adobe AIR application without resorting
Reporting services use format strings to auto format cell data. For example c2 formats
Which reporting technology would fit for the best situation/type of product? I am now
Reporting Services 2000 SP2 I've got a matrix displaying the number of employees Business1
Reporting Services 2000 Standard Edition (currently RTM but hope to have SP2 soon). I
The Reporting Database in Project Server 2007 has about 7 User Views based on
I am developing a Reporting Services solution for a DOD website. Frequently I'll have
It seems like Sql Reporting Services Server logs information in several places including web

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.