Skip to content

Instantly share code, notes, and snippets.

@stevehansen
Created March 19, 2020 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevehansen/6d7719d14b6507906a5abf3b5ffbe493 to your computer and use it in GitHub Desktop.
Save stevehansen/6d7719d14b6507906a5abf3b5ffbe493 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
namespace Unitialized
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var a = CreateA();
Debug.Assert(a.B.C.A == a);
}
private static IA CreateA()
{
var a = FormatterServices.GetUninitializedObject(typeof(A));
var b = FormatterServices.GetUninitializedObject(typeof(B));
var c = FormatterServices.GetUninitializedObject(typeof(C));
typeof(A).GetConstructor(new[] { typeof(IB) }).Invoke(a, new object[] { b });
typeof(B).GetConstructor(new[] { typeof(IC) }).Invoke(b, new object[] { c });
typeof(C).GetConstructor(new[] { typeof(IA) }).Invoke(c, new object[] { a });
return a as IA;
}
}
interface IA
{
IB B { get; }
}
interface IB
{
IC C { get; }
}
interface IC
{
IA A { get; }
}
class A : IA
{
public IB B { get; }
public A(IB b)
{
B = b;
}
}
class B : IB
{
public IC C { get; }
public B(IC c)
{
C = c;
}
}
class C : IC
{
public IA A { get; }
public C(IA a)
{
A = a;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment