简体   繁体   English

如何在Typescript中描述C#对象

[英]How to describe C# object in Typescript

We use ClearScript to extend our apps. 我们使用ClearScript扩展我们的应用程序。 We expose various C# objects into JavaScript, using ClearScript's AddHostType and AddHostObject methods. 我们使用ClearScript的AddHostType和AddHostObject方法将各种C#对象公开到JavaScript中。

Recently we've started using Typescript in our Google Apps Script projects and thought it would be good to use it to manage our ClearScript scripts as well. 最近,我们开始在我们的Google Apps脚本项目中使用Typescript,并认为也可以使用它来管理我们的ClearScript脚本。

This has proved to be an interesting experience. 事实证明,这是一个有趣的经历。 We know know about such things as external.d.ts and have started to write one to handle the various symbols that we're injecting in from the C# side. 我们知道诸如external.d.ts并且已经开始编写一个代码来处理我们从C#端注入的各种符号。

Some things we've been able to work out. 我们已经能够解决一些问题。 For example, in this code 例如,在此代码中

function SchTasks(instruction:string) {
  var proc = CSProcess.Start("C:\\Windows\\System32\\schtasks.exe", instruction);
  return proc;
}

this declaration seems to work 此声明似乎有效

declare namespace CSProcess {
  function Start(a:string, b:string): any;
}

Likewise, with this block 同样,使用此块

that.getMyIP = function () {
  var request = new CSRestRequest();
  request.AddParameter("user", username);
  request.AddParameter("pass", password);
  request.AddParameter("command", "getmyip");
  var response = client.Execute(request);
  return response.Content.trim();
};

this declaration appears to work 此声明似乎有效

declare class CSRestRequest {
    constructor (str?:any) ;
    AddParameter(a:string, b:string) : any;
}

What's puzzling us at the moment is how to declare the following. 目前困扰我们的是如何声明以下内容。 The symbol is called CSSettings and it's an instance of Dictionary<string,object> , so it has some methods hanging off it, like .ContainsKey . 该符号称为CSSettings ,它是Dictionary<string,object>的一个实例,因此它具有一些挂起它的方法,例如.ContainsKey What's the best way to declare this? 声明此的最佳方法是什么? It can't be new ed so it's not a class with a constructor, but it does have methods like a class. 它不可能是new因此它不是带有构造函数的类,但是它确实具有类似类的方法。

var taskName = "\XChecker\M_XChecker_" + (function () {
    if (CSSettings.ContainsKey("/TEST")) {
      return "TEST";
    }
    if (CSSettings.ContainsKey("/PRODUCTION")) {
      return "PRODUCTION";
    }
    return "UNKNOWN";
  }
    ()) + "_" + CSSettings("/TASKRULESETGROUPLUTFKS") + "_" + CSSettings("/INSTANCENUMBER") + "_" + CSSettings("/INSTANCECOLUMN");

LATER 后来

The following appears to work. 以下内容似乎起作用。 What's curious is that VSCode is not reporting a clash of symbol names. 奇怪的是,VSCode没有报告符号名称冲突。 Can CSSetting really be a function and a namespace at the same time? CSSetting真的可以同时成为函数和名称空间吗? Apparently, yes. 显然是的。

declare function CSSettings(s:string):any;
declare namespace CSSettings {
  function ContainsKey(s:string):boolean;
}

declare your dictionary class. 声明您的字典类。 and then variable CSSettings with this type 然后使用这种类型的变量CSSettings

declare class IDictionary {
  constructor();
  public ContainsKey(key: string): boolean;
}

declare const CSSettings: IDictionary;

CSSettings.ContainsKey('key');

// edit //编辑

so what about to create 2 type and merge it? 那么如何创建2种类型并将其合并呢? so you will be able to use CSSettings as function and even as object 这样您就可以将CSSettings用作函数甚至对象

declare type functionType = (key: string) => boolean;
declare const CSSettings: functionType & { ContainsKey: (key: string) => boolean };
CSSettings.prototype.ContainsKey = 1;


CSSettings('key');
CSSettings.ContainsKey('key');

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

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