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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:22:28+00:00 2026-06-16T20:22:28+00:00

I have this method: public int Test() { ExternalClass cls = new ExternalClass(); return

  • 0

I have this method:

public int Test()
{
    ExternalClass cls = new ExternalClass();
    return cls.ExternalMethod();
}

I need to change it using IL Injection so I can pass the type as parameter and remove the direct object creation, like this:

public int Test(ExternalClass cls)
{

    return cls.ExternalMethod();
}

I am able to add the additional parametr using <>.Parameters.Add(____) and generate the new Assembly.

The issue is that I am not able to remove the instruction for the new object creation. Below are the lines I used to remove that.

ILProcessor ilProcessor = <<MethodDefinition>>.Body.GetILProcessor();
ilProcessor.Remove(<<newobj instruction>>);

Once I tried to call the modified assembly method like below, it’s throwing the error “Common Language Runtime detected an invalid program.”

ExternalClass ext = new ExternalClass();
int i = prg1.Test(ext);

I know I may need to handle memory allocation related stuff, too. I would appreciate if anybody is able to provide additional steps to be implemented here.

  • 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-16T20:22:30+00:00Added an answer on June 16, 2026 at 8:22 pm

    You can’t just remove the newobj instruction; you’re left with an invalid program, as the runtime told you. Here’s roughly how the original method translates to IL:

    public Int32 Test()
    {
        .local ExternalClass 0;
    
        // ExternalClass cls = new ExternalClass();
        newobj class ExternalClass();
        stloc.0;
    
        // return cls.ExternalMethod();
        ldloc.0;
        callvirt Int32 class ExternalClass:ExternalMethod();
        ret;
    }
    

    If you remove only the newobj instruction then the stloc instruction will have nothing on the stack to store in the local. You need to turn the method into this:

    public Int32 Test(ExternalClass cls)
    {
        // return cls.ExternalMethod();
        ldarg.1;
        callvirt Int32 class ExternalClass:ExternalMethod();
        ret;
    }
    

    So you need to do three things:

    1. Add the argument to the method signature,
    2. Remove everything up to and including the ldloc instruction.
    3. Insert a ldarg.1 instruction at the beginning of the method.

    Optionally, you may also remove the ExternalClass local. (This will be optimized away by the JIT-compiler, but will bloat the IL image.)

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

Sidebar

Related Questions

i have a method like this: public JObject Get(int id) { return new JObject(new
I have this method to unit test: public virtual TEntity Get(int id) { return
I have this method: public CampaignCreative GetCampaignCreativeById(int id) { using (var db = GetContext())
I have this method to test : public static Date getDateSinceUTC(CstOrderBean orderBean) { int
I have this recursive method: public Hund getMor(int id) { Hund barn = getHund(id);
If I have a method like this: public void DoSomething(int Count, string[] Lines) {
I have this method: public void testJSNI2(){ String x = test; } I can
I have a web service with this method: [WebMethod] public int[] stringTest(string[] tString) {
I have this unit test: public void testDeEscapeResponse() { final String[] inputs = new
I have this demo code: class Test2 extends Test { public int number =

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.