The basic outline of my problem is shown in the code below. I'm hosting a WebBrowser control in a form and providing an ObjectForScripting with two methods: GiveMeAGizmo and GiveMeAGizmoUser. Both methods return the respective class instances:
[ComVisible]
public class Gizmo
{
public string name { get; set; }
}
[ComVisible]
public class GizmoUser
{
public void doSomethingWith(object oGizmo)
{
Gizmo g = (Gizmo) oGizmo;
System.Diagnostics.Debug.WriteLine(g.name);
}
}
In JavaScript, I create an instance of both classes, but I need to pass the first instance to a method on the second instance. The JS code looks a little like this:
var
// Returns a Gizmo instance
gizmo = window.external.GiveMeAGizmo(),
// Returns a GizmoUser instance
gUser = window.external.GiveMeAGizmoUser();
gizmo.name = 'hello';
// Passes Gizmo instance back to C# code
gUser.doSomethingWith(gizmo);
This is where I've hit a wall. My C# method GizmoUser.doSomethingWith() cannot cast the object back to a Gizmo type. It throws the following error:
Unable to cast COM object of type 'System.__ComObject' to interface type 'Gizmo'
Unsure how to proceed, I tried a couple of other things:
- Safe casting
Gizmo g = oGizmo as Gizmo;(gisnull) - Having the classes implement
IDispatchand callingInvokeMember, as explained here. The member "name" isnull.
I need this to work with .NET framework version lower than 4.0, so I cannot use dynamic. Does anybody know how I can get this working?
oGizmolook like in debugger? or is it always null?System.__ComObject. If I tryoGizmo.GetType().GetMembers(), it returnsnull(same forGetProperties()andGetMethods().