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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:02:19+00:00 2026-06-06T21:02:19+00:00

I was testing the effects of calling a virtual member in a constructor, and

  • 0

I was testing the effects of calling a virtual member in a constructor, and discovered that when calling that member the resulting exception was wrapped within a TargetInvocationException.

According to the docs this is:

The exception that is thrown by methods invoked through reflection

However I’m unaware of any invokations via reflection. So does this mean virtual members are always called via reflection? If not why is it so in this case?

The code:

class ClassA
    {
        public ClassA()
        {
            SplitTheWords();
        }

        public virtual void SplitTheWords()
        {
            //I've been overidden
        }
    }

class ClassB : ClassA
    {
        private readonly String _output;

        public ClassB()
        {
            _output = "Constructor has occured";
        }

        public override void SplitTheWords()
        {
            String[] something = _output.Split(new[]{' '}); //TargetInvocationException!
        }
    }
  • 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-06T21:02:21+00:00Added an answer on June 6, 2026 at 9:02 pm

    No, virtual methods are called via virtual dispatch.

    Reflection is not being used here. And nor is it for any virtual method calls. I believe the documentation for the exception is slightly misleading in that exceptions of this type are thrown by methods invoked via reflection, however not exclusively so.

    If anyone is curious as to why the code in the question gives an exception, it is because of the order in which the constructors are executed. The ClassB constructor is the same as:

    public ClassB() : base()
    {
        _output = "Constructor has occured";
    }
    

    Note the call to base(), this calls the base constructor before the ClassB constructor is run and, hence, before _output is assigned. The SplitTheWords virtual method is called in the base constructor, which resolves to ClassB.SplitTheWords. This method attempts to use _output, hence the error.

    For a more detailed look at why virtual methods should not be called from constructors this SO question has some useful information. Eric Lippert also has a very good blog post on why this is the case here.

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

Sidebar

Related Questions

Testing in various versions of Firefox new and old it seems that column-span in
In testing an app, I keep getting errors that occur sporadically on only one
I want to reload a page so that it does not cause the effects
I'm still working with sound effects on my test app and I noticed that
I'm currently in the process of testing our solution that has the whole gamut
I am currently testing out Ndepend, and it gives me a warning that assemblies
I am developing an application that uses CATransition to improve the transition effects between
I was testing a different feature, and I somehow, accidentally dropped an image that
testing Play framework and JPA. I eclipsify my very simple test application. Import the
Testing.BSMain, Text: Start Page I would like to substring the value above and returning

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.