C# subscribe a function to System.Action that is subscribed to another System.Action: why does order matter?

C# subscribe a function to System.Action that is subscribed to another System.Action: why does order matter?

Content Index :

C# subscribe a function to System.Action that is subscribed to another System.Action: why does order matter?
Tag : chash , By : user109285
Date : November 28 2020, 09:01 AM

will help you This is relatively apparent from examining how delegates work and the MSIL that is created when you compile this. First lets note that System.Action is a System.Delegate which has the ICloneable interface.
When you compile this program, here is pretty much what you get:
  IL_0015:  newobj     instance void [mscorlib]System.Action::.ctor(object,
                                                                    native int)
  IL_001a:  call       class [mscorlib]System.Delegate [mscorlib]System.Delegate::Combine(class [mscorlib]System.Delegate,
                                                                                          class [mscorlib]System.Delegate)
  IL_001f:  castclass  [mscorlib]System.Action
  IL_0024:  stfld      class [mscorlib]System.Action ConsoleTests.Program6/Chunk::OnTrigger
  IL_0029:  ldloc.0
  IL_002a:  ldfld      class ConsoleTests.Program6/Leaf ConsoleTests.Program6/Chunk::leaf
  IL_002f:  dup
  IL_0030:  ldfld      class [mscorlib]System.Action ConsoleTests.Program6/Leaf::OnTrigger
  IL_0035:  ldloc.0
  IL_0036:  ldfld      class [mscorlib]System.Action ConsoleTests.Program6/Chunk::OnTrigger
  IL_003b:  call       class [mscorlib]System.Delegate [mscorlib]System.Delegate::Combine(class [mscorlib]System.Delegate,
                                                                                          class [mscorlib]System.Delegate)
  IL_0040:  castclass  [mscorlib]System.Action
  IL_0045:  stfld      class [mscorlib]System.Action ConsoleTests.Program6/Leaf::OnTrigger
  IL_004a:  ldloc.0
  IL_004b:  dup
  IL_004c:  ldfld      class [mscorlib]System.Action ConsoleTests.Program6/Chunk::OnTrigger
  IL_0051:  ldnull
  IL_0052:  ldftn      void ConsoleTests.Program6::World()
  IL_0058:  newobj     instance void [mscorlib]System.Action::.ctor(object,
                                                                    native int)
  IL_005d:  call       class [mscorlib]System.Delegate [mscorlib]System.Delegate::Combine(class [mscorlib]System.Delegate,
                                                                                          class [mscorlib]System.Delegate)
  IL_0062:  castclass  [mscorlib]System.Action
  IL_0067:  stfld      class [mscorlib]System.Action ConsoleTests.Program6/Chunk::OnTrigger
  IL_006c:  ldloc.0
  IL_006d:  ldfld      class ConsoleTests.Program6/Leaf ConsoleTests.Program6/Chunk::leaf
  IL_0072:  callvirt   instance void ConsoleTests.Program6/Leaf::Go()
public Tree()
    var chunk2 = new Chunk();
    chunk2.OnTrigger += Hello; // OK: will be called
    chunk2.leaf.OnTrigger = (Action)chunk2.OnTrigger.Clone();
    chunk2.OnTrigger += World; // NOT: not be called

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

ReSharper always asks to change System.Action to System.Action<T>

Tag : chash , By : CookingCoder
Date : March 29 2020, 07:55 AM
this will help Seems to me that you need to update ReSharper to the latest version, which is version 5.1. If you have items that are not loaded by ReSharper (i.e., check your excluded items list), then it will mark them as unknown, even if your code is legal and references the items.
You may try Clear Cache, or reset default settings.
private Action ExtractFile()
    return () => MessageBox.Show("Test");

Argument 'System.Action' is not assignable to parameter type 'System.Threading.SendOrPostCallback'

Tag : chash , By : flesk
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further From MSDN.
public virtual void Send(SendOrPostCallback d, Object state)

public delegate void SendOrPostCallback(Object state)
syncContext.Send(new Action(o => { ...

Cannot convert type 'System.Action<int>' to 'System.Action<object>'

Tag : chash , By : liquidx
Date : March 29 2020, 07:55 AM
like below fixes the issue I am wondering how I achieve something like this? , Well, you could wrap the first action:
Action<object> test2 = (object o) => test((int)o);

Cannot convert from 'void' to System.Threading.Tasks.Task<System.Action>

Tag : chash , By : UnKnownUser
Date : March 29 2020, 07:55 AM
like below fixes the issue I have write generic method using hangfire .Net. Basically i want to achieve that i have generate one method and want to call it multiple times when ever it is required. My helper method is below: , I have done it by changing my methods like
 public static void ScheduleBackGroundJob(Expression<Action> _refmethod, DateTime _dateTime)
            var _currentDate = DateTime.Now;
            TimeSpan _timeSpan = _dateTime - _currentDate;
            BackgroundJob.Schedule(_refmethod, _timeSpan);
        catch (Exception ex)
            throw ex;
 HangfireHelper.ScheduleBackGroundJob(() => _service.UpdateAuctionStatus(result), result.PlannedCloseDate);
  public async Task UpdateAuctionStatus(AuctionReturnModel model)
            var _auction = await (from d in _db.Auction_Detail where d.Id == model.AuctionDetailId select d).FirstOrDefaultAsync();
            _auction.isEnded = true;

            var _auctionHistory = new AuctionHistory { AuctionDetail_Id = model.AuctionDetailId, EndedDate = DateTime.Now, EndedMethod = "Automatic" };


        catch (Exception ex)
            throw ex;

Who should subscribe to NGXS async action - the dispatch action caller or the @Action handler?

Tag : development , By : sam
Date : March 29 2020, 07:55 AM
this one helps. I think it is somewhat a matter of style, but I'd say (from my usage of NGXS) this is most typical:
On dispatch do this, and only subscribe here if there's some post-action you want to do.
Related Posts Related QUESTIONS :
  • How to get SOAP element value
  • Projection after Group
  • C# error cannot convert sytem.text.regularexpressions.match to string
  • Issues with Save/Load System in a Text Based Adventure game made with ScriptableObjects in Unity
  • VS2019 MSBuild.exe - ASP .Net MVC project fails to publish when using PublishProfile, but works when using OutDir parame
  • Does <pages validateRequest="false" /> in Web.config still matter?
  • How to send new request to redirect URL with new access token
  • Attempt to invoke virtual method on a null object reference Xamarin LockScreen
  • "The attribute names could not be inferred from bind attribute 'bind-value'" exception in Blazor
  • How to fix ''System.ArgumentException" in c#?
  • C#. Ref returning delegate for ref extension method
  • Swashbuckle Swagger generate an actual guid
  • Trying to make thousands of database calls simultaneously - they stack instead of running concurently
  • run mstest from cmd using testcontainer
  • How to make M:N (many-to-many) relationship where both M and N are the same entities?
  • Consume COM DLL from multiple clients
  • Convert json string response representing UCHAR array to Byte array
  • Why does this LinqPad program produce different results on the second run?
  • ServiceStack how to use MaxLoginAttempts feature
  • What does "late-bound access to the destination object" mean?
  • How to load appsettings.json inside a Static class based on deploy environment so that I can use in other class librarie
  • dynamically add images to view
  • Are static extension methods on User object safe?
  • Process thousands of database calls simultaneously
  • Trying to decrypt a string with public key(not private) using bouncycastle in .net
  • How to retrieve the name of an usb device
  • Query many-to-many related data using Entity Framework Core
  • ASP.NET MVC : how to find the view from a controller?
  • Inconsistent behaviour using Entity Framework for ASP.NET MVC app vs windows service
  • AutoFixture Customize vs Build
  • How to change textbox location at the click of a mouse in live mode?
  • How can I deserialize a XML file without defining the parent node?
  • asp.net core how decrease class coupling
  • ASP.NET Core Razor Pages Routing With parameters
  • String Escaping With $ Interpolation and @ Encoding
  • ConfigurationManager in ASP.NET WebAPI tested with HttpServer
  • Top down shooting in Unity 2D don't work as I want it to
  • Use own redirectUri when the Authentication expires
  • Using "foreach" in string array c#
  • "Object reference not set to an instance of an object" for create multiply models on one view
  • Get data passed trought pushAsync
  • Selector expression dynamic on IQueryable
  • Logut with SignInManager not calling method
  • Exit event handler with MessageBox OK button
  • Can not load view from Custom Renderer Xamarin Forms
  • Looking for a .Net ORM
  • Cost of SortedDictionary.Count
  • All possible combinations of n items selected randomly from a set of x items (algorithm)
  • Displaying process percentage on DOS screen
  • C# Importing Large Volume of Data from CSV to Database
  • c# control location precision
  • Printing to Zebra printer gives faint and blurry images
  • How to avoid stepping into dll code file?
  • Programmaticaly prevent Vista desktop search (WDS) from indexing pst files placed on mapped network drives
  • Is there anyway to reduce IsolatedStorage capacity in Silverlight?
  • C#: How to print a unicode string to console?
  • Frequency Table with Random Values
  • My Visual Studio 2008 web application keeps throwing a .Net error when I first run it, but refreshing fixes it
  • TCP Connection Creation and Closing Event Hooking
  • Find changed properties of a class
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com