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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:15:16+00:00 2026-06-16T03:15:16+00:00

According to my question (Reusable non generic method for generic methods) i have implemented

  • 0

According to my question (Reusable non generic method for generic methods) i have implemented the provided solution, but after some refactoring (moving code to base class) my code results in a StackOverflowException that i don´t understand.

The call to Handle(new TestCommand()) results in that IMessageHandler.Handle(IMessage) gets called and then Handle<TMessage>(TMessage) gets called, but instead of calling Handle(TestCommand message) of the inherited class it calls IMessageHandler.Handle(IMessage) again.

The as-cast to IMessageHandler<TestCommand> in the generic method works, otherwise it would call HandleUnknownMessage(IMessage).

Its hard to describe so instead here´s my TestCode:

class Program {
    static void Main(string[] args) {

        MyProcess p = new MyProcess();
        IMessageHandler handler = p;

        handler.Handle(new DummyCommand()); // works -> HandleUnknownMessage gets called as expected

        handler.Handle(new TestCommand());  // fails -> results in a StackOverflowException 
    }
}  

public abstract class ProcessBase : IMessageHandler {
    void IMessageHandler.Handle(IMessage message) {
        System.Diagnostics.Debug.WriteLine("Dynamic Message gets handled");
        dynamic dynamicMessage = message;

        Handle(dynamicMessage);
    }

    private void Handle<TMessage>(TMessage message) where TMessage : IMessage {
        System.Diagnostics.Debug.WriteLine("Generic Message gets handled");
        var handler = this as IMessageHandler<TMessage>;
        if (handler == null)
            HandleUnknownMessage(message);
        else
            handler.Handle(message);
    }

    protected virtual void HandleUnknownMessage(IMessage unknownMessage) {
        System.Diagnostics.Debug.WriteLine("Unknown message {0} passed to Process".FormatWith(unknownMessage.GetType()));
        // Handle unknown message types here.
    }
}

public class MyProcess : ProcessBase, IMessageHandler<TestCommand>, IMessageHandler<TestEvent> {

    public void Handle(TestCommand commandMessage) {
        System.Diagnostics.Debug.WriteLine("TestCommand gets handled");
    }

    public void Handle(TestEvent eventMessage) {
        System.Diagnostics.Debug.WriteLine("TestEvent gets handled");
    }
}

public class DummyCommand : IMessage {    }

public class TestCommand : IMessage {    }

If i put the code from my base class ProcessBase to MyProcess, the code works without problem. Are there any limitations in use of the dynamic keyword in conjuction with generic methods and inheritance?

  • 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-16T03:15:17+00:00Added an answer on June 16, 2026 at 3:15 am

    Try defining on IMessageHandler<TMessage> a void Handle<TMessage>(TMessage message) method.

    Since it’s not defined, your MyProcess.Handle events that you’ve defined belong only to that class and not to the interfaces it implements. When your code attempts to call on your IMessageHandler<TMessage> handler, it doesn’t know that it’s actually a MyProcess that has those specific methods. Once you add the interface method, the call resolution should recognize that those methods exist on MyProcess and call them. Otherwise it just calls the next best method being IMessageHandler.Handle(IMessage) which produces an infinite loop.

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

Sidebar

Related Questions

This is a common interview question (according to some interview sites) but I can
QUESTION 1 I have to update some fields of a table of access according
According to this question it seems like you can do this for Methods. What
According to this question I succeeded to create upload image, but now I need
I have interesting question. According to MSDN yield syntax : yield return <expression>; //
I have a question regarding a window style hexadecimal. According to http://support.microsoft.com/kb/111011/en-us , 0x16CF0000
My question is how ADSI performs SetPassword operation. According to what I have read
I have changed my code, according to the reccommendations to my previous question. Now,
According to this question , session can be edited by user. But this answer
I have modified the code in this question ,according to the answers there, in

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.