[英]Need some help converting Js to C#
我需要將其轉換為 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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.