简体   繁体   English

do.netbrowser javascript 与 c# 交互 this[string name]

[英]dotnetbrowser javascript interacting with c# this[string name]

I have a project that i am running .NET code inside.我有一个项目,我在里面运行 .NET 代码。 i need to use the existing objects and cannot modify them.我需要使用现有对象并且不能修改它们。

The class in c# that i am trying to use is我正在尝试使用的 c# 中的 class 是

class Fields
{
   Hashtable list = new Hashtable();
   public Fields this[string Name]
   {
      get{
blah blah
      }
   }
}

I am attaching the object to JS window:我将 object 附加到 JS window:

window = await _browser.MainFrame.ExecuteJavaScript<IJsObject>("window");
window.Properties["Data"] = _myObject;

when using this in the js environment, i can access everything i need to except i cannot use the getter of the Fields.在 js 环境中使用它时,我可以访问我需要的所有内容,除了我不能使用字段的 getter。

window.Data.Fields['123']

Is there something i doing wrong or is there a way to use the getter here?我做错了什么或者有没有办法在这里使用吸气剂?

You can try this approach:您可以尝试这种方法:

internal class Program
{
    public static void Main()
    {
        using (IEngine engine = EngineFactory.Create())
        {
            using (IBrowser browser = engine.CreateBrowser())
            {
                browser.Size = new Size(700, 500);

                var myObject = new MyObject();
                var window = browser.MainFrame.ExecuteJavaScript<IJsObject>("window").Result;
                window.Properties["Data"] = myObject;

                // 1. For JS, window.Data.Fields['123'] and window.Data.Fields[123] are equivalent.
                // For C# and VB, this is not the case. To be able to handle numeric indices,
                // DotNetBrowser will recognize if the parameter as a numeric index and try to look for
                // the indexer with the integer parameter, which is not present in the Fields class.
                var value = browser.MainFrame
                                            .ExecuteJavaScript("window.Data.Fields['123']")
                                            .Result;
                Console.WriteLine($"\twindow.Data.Fields['123']: {value}");

                // 2. Workaround: use the get_Item() method generated during compilation.
                // It will accept a string as a parameter and return a proper result.
                value = browser.MainFrame
                                .ExecuteJavaScript("window.Data.Fields.get_Item('123')")
                                .Result;
                Console.WriteLine($"\twindow.Data.Fields.get_Item('123'): {value}");

                // 3. The non-numeric index will be recognized as string and the corresponding
                // indexer will be used.
                value = browser.MainFrame
                                    .ExecuteJavaScript("window.Data.Fields['id']")
                                    .Result;
                Console.WriteLine($"\twindow.Data.Fields['id']: {value}");
            }
        }

        Console.WriteLine("Press any key to terminate...");
        Console.ReadKey();
    }
    class Fields
    {
        readonly Hashtable list = new Hashtable();
        public string this[string Name]
        {
            get
            {
                return list[Name]?.ToString();
            }
        }

        public void Add(string key, string value)
        {
            list[key] = value;
        }
    }
    private class MyObject
    {
        public MyObject()
        {
            Fields = new Fields();
            Fields.Add("123", "Test");
            Fields.Add("id", "Test2");
        }

        public Fields Fields { get; }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM