需要一些帮助转换Js到c#

Need some help converting Js to C#

本文关键字:Js 转换 帮助      更新时间:2023-09-26

我需要将其转换为c#,但我不确定如何做到这一点。

getParamsAsStr = function () {
    var keys = Object.keys(_self.params ? _self.params : {});
    keys.sort();
    var response = "";
    for (var i = 0 ; i < keys.length ; i++) {
        response += _self.params[keys[i]];
    }
    return response;
}

(基本上,我很想知道我应该用什么来代替Object.keys)

此函数迭代某些对象的可枚举属性(Object.keys),并将属性值写入字符串-但不包含键和任何分隔符。

我不知道_self.params在这种情况下指的是什么,因为它不是JavaScript固有的,也没有提供它的定义。

直接翻译为c#是不可能的c#/。. NET不使用具有可枚举属性的原型,最接近的模拟是将_self.params表示为Dictionary<Object,String>:

public static String GetParamsAsStr(Dictionary<Object,String> p) {
    if( p == null || p.Count == 0 ) return String.Empty;
    StringBuilder sb = new StringBuilder();
    foreach(Object key in p.Keys) sb.Append( p[key] );
    return sb.ToString();
}

我写这篇文章是为了能够把整个事情…

这是最初的JS代码,它设置了第一个参数,以便稍后进行一些API调用:

    var Signer = function () {
    this.apkId = getApkId();
    this.apkSecret = getApkSecret();
    this.servicio = "";
    this.sessionToken = "";
    this.timestamp = "";
    this.requestId = "";
    this.params = "";
    var _self = this;
    this.getParamsAsStr = function () {
        var keys = Object.keys(_self.params ? _self.params : {});
        keys.sort();
        var response = "";
        for (var i = 0 ; i < keys.length ; i++) {
            response += _self.params[keys[i]];
        }
        return response;
    }
    this.getSignature = function () {
        var baseString = 
            _self.apkSecret +
            _self.servicio +
            _self.sessionToken +
            _self.timestamp +
            _self.requestId +
            _self.getParamsAsStr();
        console.log("Signature pre hash:'n" + baseString);
        baseString = baseString.toLowerCase();
        return sha1(baseString);
    }
}
到目前为止,我在c#中做的是:
    public class Signer
{
    public string appId = getApkId();
    public string appSecret = getAppSecret();
    public string servicio = "";
    public string sessionToken = "";
    public string timestamp = "";
    public string requestId = "";
    public string params = "";
    //Here I have to write the getParamsAsStr()
    private static string getApkId(){
        string id = "xxxxxxxxxxxxxxxx";
        return id;
    }
    private static string getAppSecret(){
        string id = "xxxxxxxxxxxxxxxx";
        return id;
    }
}