简体   繁体   中英

Need some help converting Js to C#

I need to convert this to c#, but I am not sure how to do it.

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;
}

(Basically, I would love to know what should I use instead of Object.keys)

This function iterates over some object's enumerable properties ( Object.keys ) and writes out the property values to a string - though without the keys and without any delimiters.

I don't know what _self.params refers to in this context as it isn't a JavaScript intrinsic nor have you provided its definition.

A direct translation to C# is not possible as C#/.NET do not use prototypes with enumerable properties, the closest analogue is to represent _self.params as a 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();
}

I am writing this as an answer to be able to place the whole thing...

This is the original JS code which sets the first parameter to make, later on, some API calls:

    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);
    }
}

And, so far, what I did in C# is the following:

    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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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