Skip to content

Instantly share code, notes, and snippets.

@mstyura
Created March 11, 2015 20:23
Show Gist options
  • Save mstyura/4204b6beb5c1042bc53b to your computer and use it in GitHub Desktop.
Save mstyura/4204b6beb5c1042bc53b to your computer and use it in GitHub Desktop.
Prints "this is null" without ildasm/asm round-trip
using System;
using System.Runtime.Remoting.Proxies;
namespace CtorReturningNull
{
class Program
{
static void Main()
{
new Devil().SayHello();
}
}
[NullInstanceProxy]
class Devil : ContextBoundObject
{
public void SayHello()
{
if (this == null)
{
Console.WriteLine("This is null");
}
else
{
Console.WriteLine("This is not null");
}
}
}
[AttributeUsage(AttributeTargets.Class)]
class NullInstanceProxyAttribute : ProxyAttribute
{
public override MarshalByRefObject CreateInstance(Type serverType)
{
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment