简体   繁体   English

如何使用C#在JavaScript中创建的自定义对象上调用方法?

[英]How can I call a method on a custom object created in JavaScript using C#?

I have a WebBrowser control. 我有一个WebBrowser控件。 I have added some JavaScript into the head tag and I can see it is working as expected by adding an alert. 我在head标签中添加了一些JavaScript,通过添加警报可以看到它正在按预期工作。 Inside of this js I am creating a function and adding some members to it's prototype like so: 在此js内部,我正在创建一个函数并向其原型中添加一些成员,如下所示:

function test() {
}

test.prototype.run = function() {
    alert('success!')
}

function createTest() {
    return new test()
}

Then back inside of C# I am doing: 然后回到C#内部,我正在做:

dynamic test = this.browser.InvokeScript("createTest");
test.run();

I can see that the test object is some ComObject but when I call run() nothing happens. 我可以看到测试对象是一些ComObject,但是当我调用run()时,什么也没有发生。 I get no error but nothing happens. 我没有错误,但是什么也没发生。 Does anyone know how to call this type of custom object? 有谁知道如何调用这种类型的自定义对象?

Also suppose I wanted to get rid of the createTest() method, how can I create a new instance of test from C#? 还要假设我想摆脱createTest()方法,如何从C#创建一个新的测试实例?

Also, for bonus points, is there anything special I need to know about attaching events to this custom object (on say a 'complete' member) such that it will callback into my C# code? 另外,为了获得加分,关于将事件附加到此自定义对象(例如“完整”成员),以便回调到我的C#代码中,我是否需要了解什么特别的信息?

尝试将空的System.object数组作为InvokeScript的第二个参数传递,然后调用test.run();。

Turns out that the "dynamic" keyword isn't smart enough to figure this out for some reason. 事实证明,由于某些原因,“动态”关键字不够聪明,无法找出答案。 What you can do is cast the COM object returned from InvokeScript into IExpando (or IReflect) and call the InvokeMember method. 您可以做的是将InvokeScript返回的COM对象转换为IExpando(或IReflect),然后调用InvokeMember方法。 I created a class that inherits from DynamicObject that overrides all TryXYZ methods and transforms them into IReflect/IExpando method calls. 我创建了一个从DynamicObject继承的类,该类重写所有TryXYZ方法并将其转换为IReflect / IExpando方法调用。 Then you can interact with the object like the above snippet. 然后,您可以像上面的代码片段那样与对象进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中的方法调用中传递对象实例? - How can I pass an object instance in a method call in C#? 如何在StringTemplate中的对象上调用C#扩展方法? - How can I call a C# extension method on an object in StringTemplate? 如何使用jQuery调用C#方法? - How can I call a C# method using jQuery? 如何在没有WebMethod或Updatepanel的情况下通过javascript调用服务器端c#方法? - How can I call a server side c# method via javascript without a WebMethod or Updatepanel? 我如何获取此javascript来调用我的C#方法并传递参数 - How can I get this javascript to call my C# method and pass a Parameter 如何在C#中刮取用JavaScript创建的表 - How can I scrape a table that is created with JavaScript in c# 如何使用 javascript 调用 ASP.NET c# 方法 - how to call an ASP.NET c# method using javascript 如何使用GeckoFX作为XULRunner的包装器在javascript中调用C#方法 - How to call C# method in javascript by using GeckoFX as the wrapper of XULRunner 如何在 .NET Framework 4.7.2 中使用 JavaScript 调用 C# 方法? - How do I call a C# method using JavaScript in .NET Framework 4.7.2? 在 C# 中,当我使用 object 作为参数时,如何在 object 上执行方法? - In C# how can I execute a method on an object when I am using that object as a parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM