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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:08:04+00:00 2026-06-13T03:08:04+00:00

I tried to unsubscribe from a lambda within itself. I used the MethodInfo class

  • 0

I tried to unsubscribe from a lambda within itself. I used the MethodInfo class for getting information about a lambda and the Delegate.CreateDelegate method for creating the same method as a lambda. So it works fine if a lambda created in one of the class methods that contains the event i use but doesn’t work in another class method (binding exception).

Here’s the code:

public class TestClass
{
    public event Action SomeEvent;
    public void OnSomeEvent()
    {
        if (SomeEvent != null)
        {
            SomeEvent();
        }
    }
    public TestClass()
    {
        //IT WORKS FINE!!!
        //SomeEvent += () =>
        //{
        //    Console.WriteLine("OneShotLambda");
        //    MethodInfo methodInfo = MethodInfo.GetCurrentMethod() as MethodInfo;
        //    Action action = (Action)Delegate.CreateDelegate(typeof(Action), this, methodInfo);
        //    SomeEvent -= action;
        //};
    }
}

class Program
{
    static void Main(string[] args)
    {
        TestClass t = new TestClass();
        t.SomeEvent += () =>
            {
                Console.WriteLine("OneShotLambda");
                MethodInfo methodInfo = MethodInfo.GetCurrentMethod() as MethodInfo;
                //GOT AN ERROR
                Action action = (Action)Delegate.CreateDelegate(typeof(Action), t, methodInfo);
                t.SomeEvent -= action;
            };
        t.OnSomeEvent();
        t.OnSomeEvent(); //MUST BE NO MESSAGE
    }
}
  • 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-13T03:08:06+00:00Added an answer on June 13, 2026 at 3:08 am

    The reason why it doesn’t work is that you pass in t as the second parameter to CreateDelegate when you should pass null. This is due to the fact that the method created for the lambda will be a static method. Otherwise it couldn’t be used from the static Main method. And the documentation clearly states that you need to pass null for static methods.

    Having said that, you would be better off doing it differently:

    TestClass t = new TestClass();
    Action eventHandler = null;
    eventHandler = () =>
                   {
                       Console.WriteLine("OneShotLambda");
                       t.SomeEvent -= eventHandler;
                   }
    t.SomeEvent += eventHandler;
    

    This code is more readable and understandable and additionally performs better.

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

Sidebar

Related Questions

Tried to configure Spring for tests with hibernate and transactions. Getting bean from app
tried uncommenting pam_limits.so from the pam.d directory but no luck. Basic PAM seems to
Tried examples from 'php.net' but don't understand what's the problem. Any suggestions? <?php $_SESSION['test']
Tried to bind submit event (or click or whatever) to an element within a
Tried to reuse Sum but got this error: cannot resolve method Sum how can
Tried to solve the problem particularly with rubycas-server which is based on sinatara Getting
Tried to create this Extension method. Works except that the helper is rendering text,
I'm getting the following error whilst running this script. I've tried using the following:
Tried this : myStructs = from MyObject s in MyObjects join c in Categories
Tried to look at all the other questions about why this isn't working, no

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.