Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,007 | Comments: 44,762

filter by tags archive

Can you hack this out? Hint #1

time to read 2 min | 252 words

Here is another challenge, smaller, this time, which can lead you the right path.  And yes, it is a screwy one.

All you have to do is make the following assertion fail.

public class Program
private static void Main(string[] args)

public static void CanThisHappen<T>() where T : class, new()
var instnace = new T();
Debug.Assert(instnace != null,"How did we break the CLR?");

The rules are simple, you are not allowed to do anything outside the current process. That means that profiling API, post compilation assembly rewrite, etc are forbidden. Anything else is a fair game.

I will let you know that I can make the assertion fail in 4 lines of code :-)



perhaps like this:

public class Program {

private static void Main() {




public static void CanThisHappen

<t() where T : class, new() {

    var instnace = new T();

    Debug.Assert(!instnace.Equals(null), "How did we break the CLR?");



internal class MyFunnyType {

public override bool Equals(object obj) {

    return true;



Ayende Rahien


That would work, yes, but that doesn't match the challenge that I set.


Can we modify CanThisHappen method of program?

Marc Gravell

class MyFunnyTypeAttribute : ProxyAttribute {

public override MarshalByRefObject CreateInstance(Type serverType) { return null; }


[MyFunnyType] class MyFunnyType : ContextBoundObject{}

Ayende Rahien




Once the previous hack was solved, this is pretty easy.


Do you mean to break

var instnace = new <whatever;

object.ReferenceEquals(instnace, null) == false?


Damn, thats a NASTY solution :S


I did not know that ProxyAttribute exists :)

It opens so many possibilities!!!

Reshef Mann

Apparently this will do the trick:

        public static bool operator !=(object left, MyFunnyType right)


            return false;

Ayende Rahien


Try that, it won't work :-)


Right. When creating the object with new T() somehow the type is unknown and it won't call the != operator. Generics oddity...

Marc Gravell

@Reshef - it isn't that the T is unknown; it is that the operator can't be resolved via static analysis (which it must) to a single implementation. Similar to how +-*/ don't work (unless you cheat, like in MiscUtil's Operator class). I suspect there is some scenarios (involving subclassing and a constraint like T : SomeType) where it /would/ pick up the operator, but that ("T : SomeType") isn't in the question.


Looks like this would work.

public class InterceptionProxy : RealProxy


    public InterceptionProxy() : base(typeof(MyFunnyType)) { }

     public override IMessage Invoke(IMessage msg)


         // Does not get here



    public override object GetTransparentProxy()


        return null;



Chris Wright

I've used a programming language in which you can return a value from a constructor, even null. Insanity :)

Mark Rogers

Cool post, I hate to say but I think you misspelled 'instance' as 'instnace' in your code example.



Ooh, nice solution. I was thinking about this:

using MyFunnyType = System.Nullable <int;

As far as I recall, Nullable structs are not applicable for the "struct" constraint (which is actually defined as "non-nullable value type"), so I guess they might be applicable for the "class" constraint. I'm probably wrong, but it'd be a nice solution.

Comment preview

Comments have been closed on this topic.


No future posts left, oh my!


  1. Speaking (3):
    23 Sep 2015 - Build Stuff 2015 (Lithuania & Ukraine), Nov 18 - 24
  2. Production postmortem (11):
    22 Sep 2015 - The case of the Unicode Poo
  3. Technical observations from my wife (2):
    15 Sep 2015 - Disk speeds
  4. Find the bug (5):
    11 Sep 2015 - The concurrent memory buster
  5. Buffer allocation strategies (3):
    09 Sep 2015 - Bad usage patterns
View all series



Main feed Feed Stats
Comments feed   Comments Feed Stats