electron调用c#动态库

electron调用c#动态库

新建C#动态库

  • 方法要以异步任务的方式,可以直接包装,也可以写成天然异步

  • 代码如下

    public class Class1
        {
            public async Task Invoke(object input)
            {
                return Helper.SayHi("Invoke1:" + (string)input);
            }
    
            public async Task Invoke2(object input)
            {
                return  Helper.SayHi("Invoke2:" + (string)input);
            }
    
            static class Helper
            {
                public static string SayHi(string param)
                {
                    return ".NET Welcomes " + param;
                }
            } 
      
     

    安装electron-edge-js模块

    • 调用代码如下

      
      const edge = require('electron-edge-js');
      console.info("call c#")
      
      var DemoDll = edge.func({
          assemblyFile: "electronedge.dll",
          typeName: "electronedge.Class1",
          methodName: "Invoke"
      });
      var DemoDll2 = edge.func({
          assemblyFile: "electronedge.dll",
          typeName: "electronedge.Class1",
          methodName: "Invoke2"
      });
      // module.exports.DemoDll = DemoDll;
      module.exports.DemoDll = {
          demo: DemoDll,
          demo2:DemoDll2
      };
      
      
    • node引用如下

      const DemoDll = require("./csharputil.js");
      
      DemoDll.DemoDll.demo("test", (err, value)=> {
        log.debug(value);
      
      });
    • 页面js引用如下

      • 包装如下

        function init() {
            const DemoDll = require("F:/yanghuaihua/electronedge/csharputil.js");
        
        
            return {
                demo: DemoDll.DemoDll.demo,
                demo2:DemoDll.DemoDll.demo2
        
            };
        }
        const initRequire = init();
      • 引用如下

        
            
            
            
            

    源码地址

    https://github.com/Ants-double/yumi/tree/master/electronedge

    你可能感兴趣的:(electron调用c#动态库)