Runtime InvalidCastException with implicit cast operator

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):

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 compiler can find the cast operator but it's lost at runtime. Surely the VB and C# IL will be pretty similar here?

The 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?

13.10.2009 15:23:43
Dim b as Bar = GetBar() Try to use this way: Dim b = GetBar()
Restuta 13.10.2009 15:28:59
Opps, delete a comment above, just missed a point.
Restuta 13.10.2009 15:29:48

First, I'd try and mark a C# assembly as CLSCompliant(true) to see if this generates any warnings on implicit operator Foo.

Aha, here it is:

The problem is that VB.NET simply doesn't call op_Implicit/op_Explicit functions exposed by the C# code. Delving into the Visual Basic engine you can see that under the covers it use ICovertible to do all of its conversions.

13.10.2009 15:29:25
IConvertible does all the system value type conversions. I thought that in VB.Net you could do Public Shared Widening Operator CType(ByVal input As Bar) As Foo and it would be the same as the public static implicit operator Foo ( Bar input )
Keith 14.10.2009 07:22:37
@Anton, Good find man. Since VB doesn't call the op_Implicit functions it looks like VB will never be able to call these things in the same way as C#. Which is crazy, and yet another reason I won't touch VB.Net unless I need to...(Too many quirks with it and things C# supports, but it doesn't...)
Jason D 15.12.2009 02:33:42
I don't the above link works, here is revised link;…
pkr2000 25.03.2013 21:19:20