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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:10:54+00:00 2026-06-15T18:10:54+00:00

Foreword: I am trying to describe the scenario very precisely here. The TL;DR version

  • 0

Foreword: I am trying to describe the scenario very precisely here. The TL;DR version is ‘how do I tell if a lambda will be compiled into an instance method or a closure’…

I am using MvvmLight in my WPF projects, and that library recently changed to using WeakReference instances in order to hold the actions that are passed into a RelayCommand. So, effectively, we have an object somewhere which is holding a WeakReference to an Action<T>.

Now, since upgrading to the latest version, some of our commands stopped working. And we had some code like this:

ctor(Guid token)
{
    Command = new RelayCommand(x => Messenger.Default.Send(x, token));
}

This caused a closure (please correct me if I’m not using the correct term) class to be generated – like this:

[CompilerGenerated]
private sealed class <>c__DisplayClass4
{
    public object token;

    public void <.ctor>b__0(ReportType x)
    {
        Messenger.Default.Send<ReportTypeSelected>(new ReportTypeSelected(X), this.token); 
    }
}

This worked fine previously, as the action was stored within the RelayCommand instance, and was kept alive whether it was compiled to an instance method or a closure (i.e. using the ‘<>DisplayClass’ syntax).

However, now, because it is held in a WeakReference, the code only works if the lambda specified is compiled into an instance method. This is because the closure class is instantiated, passed into the RelayCommand and virtually instantly garbage collected, meaning that when the command came to be used, there was no action to perform. So, the above code has to be modified. Changing it to the following causes that, for instance:

Guid _token;
ctor(Guid token)
{
    _token = token;
    Command = new RelayCommand(x => Messenger.Default.Send(x, _token));
}

This causes the compiled code to result in a member – like the following:

[CompilerGenerated]
private void <.ctor>b__0(ReportType x)
{
    Messenger.Default.Send<ReportTypeSelected>(new ReportTypeSelected(X), this._token);
}

Now the above is all fine, and I understand why it didn’t work previously, and how changing it caused it to work. However, what I am left with is something which means the code I write now has to be stylistically different based on a compiler decision which I am not privy to.

So, my question is – is this a documented behaviour in all circumstances – or could the behaviour change based on future implementations of the compiler? Should I just forget trying to use lambdas and always pass an instance method into the RelayCommand? Or should I have a convention whereby the action is always cached into an instance member:

Action<ReportTypeSelected> _commandAction;
ctor(Guid token)
{
    _commandAction = x => Messenger.Default.Send(x, token);
    Command = new RelayCommand(_commandAction);
}

Any background reading pointers are also gratefully accepted!

  • 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-15T18:10:56+00:00Added an answer on June 15, 2026 at 6:10 pm

    Whether you will end up with a new class or an instance method on the current class is an implementation detail you should not rely on.

    From the C# specification, chapter 7.15.2 (emphasis mine):

    It is explicitly unspecified whether there is any way to execute the block of an anonymous function other than through evaluation and invocation of the lambda-expression or anonymous-method-expression. In particular, the compiler may choose to implement an anonymous function by synthesizing one or more named methods or types.

    -> Even the fact that it generates any methods at all is not specified.

    Given the circumstances, I would go with named methods instead of anonymous ones. If that’s not possible, because you need to access variables from the method that registers the command, you should go with the code you showed last.


    In my opinion the decision to change RelayCommand to use WeakReference was a poor one. It created a lot more problems than it solved.

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

Sidebar

Related Questions

Current demo here: http://www.studioimbrue.com/sites/eliotdudik I have the tabs system working wonderfully. I'm trying to
Foreword: I am not trying to write an alternative either to Subversion or to
Foreword Before asking a question that probably will pretend to downvoting (as it seems
Edit: Updated code. Now works well enough. Foreword. I am an OpenGL newb tasked
I am new to Android and trying to setup Android Development Environment. I have
I'm trying to use internal references to link an index atop my wiki page
foreword: I have a component, lets call it IView. This component is implemented by
Foreword: Although I don't think it's exactly a duplicate, feel free to close if
Foreword tl;wr: This is a discussion. I am aware that this question is more
Foreword: I want to allow users to define high quality document templates and then

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.