我有兩組Url,一個用於PreProd,一個用於Prod。每個Url都有幾個API節點。而不是硬編碼這些API節點,我保持他們在一個枚舉從字符串方法名稱調用方法
事情是這樣的:
//Prod
private enum Prod
{
precheckorder,
submitresubmit,
creditInquiry,
createupdateorder,
confirmorder,
getorderstatus,
cancelorder,
}
/// <summary>
/// Gets the relative URL.
/// </summary>
/// <param name="u">The u.</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private static string GetRelativeUrl(Prod u)
{
switch (u)
{
case Prod.precheckorder:
return "https://contesa.tex.com/api/precheckorder";
case Prod.submitresubmit:
return "https://contesa.tex.com/api/submitresubmit";
case Prod.creditInquiry:
return "https://contesa.tex.com/api/creditinquiry";
case Prod.createupdateorder:
return "https://contesa.tex.com/api/createupdateorder";
case Prod.confirmorder:
return "https://contesa.tex.com/api/confirmorder";
case Prod.getorderstatus:
return "https://contesa.tex.com/api/getorderstatus";
case Prod.cancelorder:
return "https://contesa.tex.com/api/cancelorder";
default:
// Handle bad URL, possibly throw
throw new Exception();
}
}
我們使用環境變量來存儲環境的名字和那是什麼決定了要使用的API集。
理想情況下,我想要一個方法,我傳入我的環境和API名稱,它將返回API Url。
喜歡的東西
GettexApiUrlBasedOnEnvironment("Dev", "precheckorder");
和響應將是
"https://contoso.tex.com/api/precheckorder"
任何意見/建議,我們將不勝感激。 TIA
目前尚不清楚你想要做什麼。如果你只是想要返回一個方法,那麼只需要兩個帶重載的方法,其中輸入參數是枚舉類型並執行方法內的邏輯(例如'string MyMethod(EnumType1 en)'和'string MyMethod(EnumType2 EN)「)。另外,我在您展示的Gettex ...方法中看到「Dev」一詞,但在問題的其他地方沒有。你只是試圖根據你是否在release/debug模式下運行來改變結果嗎?如果是這樣,只需使用'#if DEBUG'並在程序加載時放入一些邏輯。什麼是'反射'標籤? – beeker