c# - InvalidCastException when casting dynamically to interface -
i have interface defined in assembly this:
namespace helloworlder { public interface ihelloworld { string sayhello(); } }
in seperate assembly have class defined as:
using helloworlder; public class helloworld : ihelloworld { string ihelloworld.sayhello() { return "hello, world!"; } }
now trying invoke f# interactive invalid cast exception, here code trying consume dll:
#i __source_directory__ #r "helloworlder.dll" open system open system.reflection open helloworlder let asm = assembly.loadfrom(__source_directory__ + "\\helloworld.dll") let hwins = asm.createinstance("helloworld") :?> ihelloworld let res = hwins.sayhello()
from can see should work, precaution see have implemented interface ihelloworld
explicitly. i'm guessing there i'm missing cannot figure out why it's throwing cast exception.
it turns out had unbox object instead of casting it, , worked fine.
let hwins = asm.createinstance("helloworld") |> unbox<ihelloworld>
Comments
Post a Comment