Reputation: 155872
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?
Upvotes: 2
Views: 1071
Reputation: 115867
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 useICovertible
to do all of its conversions.
Upvotes: 1