I think I found a bug in .NET 2.0 (not sure about 3.0 and 3.5) but I can't see to find any articles about this. I am working on something that heavily involves reflection, calling functions in classes that do not exist during compile time, etc, etc. As I was doing this, I found this interesting....quirk:
So check this out. I am going to show the code for reproducing the bug as well as explain it. So. All classes, types and such are derived from System.Object. For example:
System.String -> System.Object
System.IO.MemoryStream -> System.IO.Stream -> System.MarshalByRefObject -> System.Object
So, you get my point. Now check this out:
System.Int32 -> System.ValueType -> System.Object
If I create a delegate whose return type is System.Object and then return any instance of System.String, System.IO.MemoryStream...then .NET has absolutely no problems. If you will notice that System.IO.MemoryStream is 3 levels deep in inheritance.
So, my delegate has a return type of System.Object, but when I implement this delegate and the return type is System.String or System.IO.MemoryStream or System.GrandmaSpecialCake, we're all fine and dandy.
HOWEVER
If I try to return a System.ValueType such as System.Int32 or System.Double, .NET has a hissy fit. No no no, you can't do that. Because.....pffft, fuck you. System.Int32 is only 2 levels deep into inheritance from System.Object.
Let me show some code:
// Declare some delegate with its return type as System.Object
public delegate object SomeDelegate();
SomeDelegate myDelegate = new SomeDelegate(FunctionReturnsString);
...
myDelegate();
...
// .NET Has Not Problems with This:
public String FunctionReturnsString()
{
return "123";
}
SomeDelegate myDelegate = new SomeDelegate(FunctionReturnsStream);
...
myDelegate();
...
// .NET Has Not Problems with This:
public System.IO.MemoryStream FunctionReturnsStream()
{
return new MemoryStream();
}
SomeDelegate myDelegate = new SomeDelegate(FunctionReturnsInt32);
...
myDelegate();
...
// .NET Shits Its Pants:
public Int32 FunctionReturnsInt32()
{
return 123;
}
So yeah. Interesting stuff. Don't know if this still happens in .NET 3 or 3.5.
EDIT: I should mention that the exception occurs when I am creating the delegate that points to these functions.
[Edited by - Programmer One on June 11, 2008 6:01:20 PM]