2
\$\begingroup\$

The class System.Globalization.EastAsianLunisolarCalendar of the mscorlib.dll assembly is non-nested public but its sole instance constructor is internal and it also declares some internal abstract members.

The problem: Write a C# class in your own assembly (code must compile with usual C# compiler) where a class derives from (inherits) the System.Globalization.EastAsianLunisolarCalendar of the BCL.

Your class must have System.Globalization.EastAsianLunisolarCalendar as its direct base class. It is not allowed to have a third class in "between" your class and that class in the inheritance hierarchy.

(Advanced stuff like dynamic assemblies or runtime IL emission is not needed.)

(Stated as a C# problem. Discussions on whether the equivalent problem can be solved in other object-oriented languages too, will also be considered interesting.)

It is not required that the class you write is usable in any way ;-)

\$\endgroup\$
  • \$\begingroup\$ Did not have enough reputation to create tags encapsulation, inheritance, access-level. \$\endgroup\$ – Jeppe Stig Nielsen Nov 28 '13 at 11:34
  • 3
    \$\begingroup\$ You should come up with an objective winning criteria and post as a code challenge \$\endgroup\$ – Cruncher Nov 28 '13 at 16:16
  • \$\begingroup\$ @Cruncher Put in code-challenge category. Not sure about winning criterion but I guess the first post to give a valid solution wins. \$\endgroup\$ – Jeppe Stig Nielsen Nov 28 '13 at 17:55
  • \$\begingroup\$ -1: no objective winning criterion and changed question fundamentally after a valid answer was given. \$\endgroup\$ – Howard Nov 29 '13 at 7:28
  • \$\begingroup\$ Looks like "first valid answer wins". \$\endgroup\$ – Johannes Kuhn Nov 29 '13 at 8:30
5
\$\begingroup\$

Well, I have either to call a base constructor (which is not accessible) or an other constructor in the same class.

public abstract class Class1 : System.Globalization.EastAsianLunisolarCalendar {
    private Class1() : this(1) {}
    private Class1(int i) : this() {}
}

So let's call the constructors recursive. Not very useful, but who cares?

\$\endgroup\$
  • \$\begingroup\$ Yes, this was the answer I had in mind. Too bad my original phrasing didn't exclude w0lf's solution, as he was first. I still award the "accepted" check mark to this answer. \$\endgroup\$ – Jeppe Stig Nielsen Nov 29 '13 at 8:30
  • \$\begingroup\$ Afaik, you could even intercept the instance with a destructor... \$\endgroup\$ – Johannes Kuhn Nov 16 '14 at 18:23
  • \$\begingroup\$ Will the instance ever be eligible for garbage collection? Do you terminate the thread running in an infinite loop (jumping between the two constructors)? Possibly another way to get an instance is System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(Class1)). \$\endgroup\$ – Jeppe Stig Nielsen Nov 16 '14 at 18:53
  • \$\begingroup\$ codeblog.jonskeet.uk/2014/10/23/… \$\endgroup\$ – Johannes Kuhn Nov 16 '14 at 20:35
  • \$\begingroup\$ Interesting post. If this cyclic constructor chain hack was new to Jon Skeet, he should have been reading more of your answers at Programming Puzzles & Code Golf Stack Exchange ;-) \$\endgroup\$ – Jeppe Stig Nielsen Nov 17 '14 at 6:37
3
\$\begingroup\$

One way to do it is to inherit a subclass of System.Globalization.EastAsianLunisolarCalendar:

public class MySubclass : System.Globalization.JapaneseLunisolarCalendar
{
    public static void Test()
    {
        var instance = new MySubclass();

        Console.WriteLine(instance is System.Globalization.EastAsianLunisolarCalendar);
        // this prints "True"
    }
}
\$\endgroup\$
  • \$\begingroup\$ Damn, will have to upvote this because my problem text didn't exclude this solution. However, the intention was to require EastAsianLunisolarCalendar to be the direct base class. Will update question. Sorry I won't award this question a winner. \$\endgroup\$ – Jeppe Stig Nielsen Nov 29 '13 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.