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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:49:41+00:00 2026-05-24T03:49:41+00:00

I was suprised when found that the following code throws exception at runtime: class

  • 0

I was suprised when found that the following code throws exception at runtime:

class A
{
    public string Name { get; set; }

    public A()
    {
        Name = "Class A";
    }
}

class B
{
    public string Name { get; set; }

    public B()
    {
        Name = "Class B";
    }

    public static explicit operator A(B source)
    {
        return new A() {Name = source.Name};
    }
}


class Program
{
    static void Main(string[] args)
    {
        // This executes with no error
        var bInstance = new B();
        Console.WriteLine(bInstance.GetType()); // <assemblyname>.B
        var aInstance = (A) bInstance;
        Console.WriteLine(aInstance.Name); // Class B

        // This fails with InvalidCastException
        var bInstanceReflection = Activator.CreateInstance(typeof (B));
        Console.WriteLine(bInstanceReflection.GetType()); // <assemblyname>.B
        var aInstanceReflection = (A) bInstanceReflection;

        Console.WriteLine(aInstanceReflection.Name);
    }
}

Could anyone tell me why? I don’t really understand what happened

  • 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-05-24T03:49:42+00:00Added an answer on May 24, 2026 at 3:49 am

    You shouldn’t be surprised – custom operators don’t override anything, they overload – so they’re picked at compile time, not execution time.

    When we remove implicit typing from the code, it makes it a bit clearer:

    object bInstanceReflection = Activator.CreateInstance(typeof (B));
    Console.WriteLine(bInstanceReflection.GetType()); // <assemblyname>.B
    A aInstanceReflection = (A) bInstanceReflection;
    

    Now it’s reasonably clear that in the final line, (A) is just a cast from object which performs the normal reference conversion. No user-defined conversions will be applied at all.

    If you’re using .NET 4, you can use dynamic typing to get it to work:

    // Note the change of type
    dynamic bInstanceReflection = Activator.CreateInstance(typeof (B));
    Console.WriteLine(bInstanceReflection.GetType()); // <assemblyname>.B
    A aInstanceReflection = (A) bInstanceReflection;
    

    Now the conversion is being applied on a dynamic value, which means the choice of what conversion to use is deferred until execution time – at which point it will use your custom operator.

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

Sidebar

Related Questions

I found out that the following code gets accepted by Visual C++ 2008 and
After writing code that can be boiled down to the following: var size=-1; var
Suppose the following template definition (the code is meaningless): template<class X, class Y> bool
We've set the following property in persistence.xml: <property name=openjpa.jdbc.SynchronizeMappings value=buildSchema(ForeignKeys=true)/> If I understand correctly,
Later today, I was scrolling through ejhon.com slides and I found out the following:
I was talking to a friend of mine that through my new code I
I'm trying to write code that talks to Google Spreadsheets. We do a bunch
I am not able to compile the following code. I run it in a
I found that different compiler optimization levels in gcc give quite different results when
I was surprised recently to find that it's possible to have a return statement

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.