I have a C# library that internal clients configure with VB.Net
Their scripts are throwing an InvalidCastException
where they really shouldn't.
So the code is something like this (massively simplified):
//C#3
public class Foo {
public static implicit operator Foo ( Bar input )
{
return new Foo( input.Property1, input.Property2 );
}
}
Then in their VB.Net (again massively simplified):
Dim fc = New FooCollection()
Dim b as Bar = GetBar()
fc(fooIndex) = b 'throws InvalidCastException at runtime!
If I add a breakpoint inside the implicit/widening operator it's never reached.
If I remove the implicit operator it won't compile.
If I execute the equivalent statement in C#:
var fc = new FooCollection();
Bar b = GetBar();
fc[fooIndex] = b //it works!
Strange - it looks like the VB.net compiler can find the cast operator but it's lost at runtime. Surely the VB and C# IL will be pretty similar here?
The VB.net code is dynamically compiled - the compile happens the first time a user logs into the app. it's being compiled as VB.Net against .Net 3.5, and I'm not using any COM interop.
Any ideas?