Skip to main content
Fixed minor syntactic mistake.
Source Link

Well, you could do what you want with some relatively "grey" magic, although I have not done this specific thing myself in the past. By the way, you still have to decide what code you will replace the default constructor with. If I understand well what you are trying to do, you would want the default constructor throwing an ArgumentException, is that right?

If you have some patience and a bit of spare time (not too much, really), the IL Assembler does not appear to have any problem with an explicit default parameterless constructor. As Guess Who* "successfully" (ok, no serious customization was performed anyway) poked around a long time ago, it appears to be feasible to do whatever you want inside an empty constructor for a custom type extending ValueType. You can use the article for a bit of guidance.

So, what I would try is:

  • Create an additional constructor in your almost-done type, receiving a dummy parameter (say, an int) and throwing an ArgumentException with the text informing that the the default empty constructor is not intended to be called directly (or any other exception you see fit).
  • "Bake" the almost-done type in a class library alone.
  • Disassemble the library back to CIL.
  • Read and understand a few basics of CIL so that I can...
  • ...remove the dummy int parameter from my constructor without causing other side effects in the code, so it would become the empty constructor.
  • Reassemble back using the ILAssembler directly from the disassembled, tampered IL code.

Then, boom, magically, youI can never create an empty array of pre-initialized NonEmptyStrings anymore (for example NonEmptyString[] strings = new NonEmptyString[100]).

I assume this is grey area and you could feel better going with your bool solution anyway, but if you decide to give this a shot, I would very much like to know how this worked out.

*Also known as Jon Skeet!

Well, you could do what you want with some relatively "grey" magic, although I have not done this specific thing myself in the past. By the way, you still have to decide what code you will replace the default constructor with. If I understand well what you are trying to do, you would want the default constructor throwing an ArgumentException, is that right?

If you have some patience and a bit of spare time (not too much, really), the IL Assembler does not appear to have any problem with an explicit default parameterless constructor. As Guess Who* "successfully" (ok, no serious customization was performed anyway) poked around a long time ago, it appears to be feasible to do whatever you want inside an empty constructor for a custom type extending ValueType. You can use the article for a bit of guidance.

So, what I would try is:

  • Create an additional constructor in your almost-done type, receiving a dummy parameter (say, an int) and throwing an ArgumentException with the text informing that the the default empty constructor is not intended to be called directly (or any other exception you see fit).
  • "Bake" the almost-done type in a class library alone.
  • Disassemble the library back to CIL.
  • Read and understand a few basics of CIL so that I can...
  • ...remove the dummy int parameter from my constructor without causing other side effects in the code, so it would become the empty constructor.
  • Reassemble back using the ILAssembler directly from the disassembled, tampered IL code.

Then, boom, magically, you can never create an empty array of pre-initialized NonEmptyStrings anymore (for example NonEmptyString[] strings = new NonEmptyString[100]).

I assume this is grey area and you could feel better going with your bool solution anyway, but if you decide to give this a shot, I would very much like to know how this worked out.

*Also known as Jon Skeet!

Well, you could do what you want with some relatively "grey" magic, although I have not done this specific thing myself in the past. By the way, you still have to decide what code you will replace the default constructor with. If I understand well what you are trying to do, you would want the default constructor throwing an ArgumentException, is that right?

If you have some patience and a bit of spare time (not too much, really), the IL Assembler does not appear to have any problem with an explicit default parameterless constructor. As Guess Who* "successfully" (ok, no serious customization was performed anyway) poked around a long time ago, it appears to be feasible to do whatever you want inside an empty constructor for a custom type extending ValueType. You can use the article for a bit of guidance.

So, what I would try is:

  • Create an additional constructor in your almost-done type, receiving a dummy parameter (say, an int) and throwing an ArgumentException with the text informing that the the default empty constructor is not intended to be called directly (or any other exception you see fit).
  • "Bake" the almost-done type in a class library alone.
  • Disassemble the library back to CIL.
  • Read and understand a few basics of CIL so that I can...
  • ...remove the dummy int parameter from my constructor without causing other side effects in the code, so it would become the empty constructor.
  • Reassemble back using the ILAssembler directly from the disassembled, tampered IL code.

Then, boom, magically, I can never create an empty array of pre-initialized NonEmptyStrings anymore (for example NonEmptyString[] strings = new NonEmptyString[100]).

I assume this is grey area and you could feel better going with your bool solution anyway, but if you decide to give this a shot, I would very much like to know how this worked out.

*Also known as Jon Skeet!

Source Link

Well, you could do what you want with some relatively "grey" magic, although I have not done this specific thing myself in the past. By the way, you still have to decide what code you will replace the default constructor with. If I understand well what you are trying to do, you would want the default constructor throwing an ArgumentException, is that right?

If you have some patience and a bit of spare time (not too much, really), the IL Assembler does not appear to have any problem with an explicit default parameterless constructor. As Guess Who* "successfully" (ok, no serious customization was performed anyway) poked around a long time ago, it appears to be feasible to do whatever you want inside an empty constructor for a custom type extending ValueType. You can use the article for a bit of guidance.

So, what I would try is:

  • Create an additional constructor in your almost-done type, receiving a dummy parameter (say, an int) and throwing an ArgumentException with the text informing that the the default empty constructor is not intended to be called directly (or any other exception you see fit).
  • "Bake" the almost-done type in a class library alone.
  • Disassemble the library back to CIL.
  • Read and understand a few basics of CIL so that I can...
  • ...remove the dummy int parameter from my constructor without causing other side effects in the code, so it would become the empty constructor.
  • Reassemble back using the ILAssembler directly from the disassembled, tampered IL code.

Then, boom, magically, you can never create an empty array of pre-initialized NonEmptyStrings anymore (for example NonEmptyString[] strings = new NonEmptyString[100]).

I assume this is grey area and you could feel better going with your bool solution anyway, but if you decide to give this a shot, I would very much like to know how this worked out.

*Also known as Jon Skeet!