我有一個異步函式,它必須被多次呼叫,但每次呼叫都會改變一個輸入引數。
public void ProcesAsyncReq((Class) validatedInputs, string data1, string data2...)
{
//logic here is to just pass the validatedInputs to the dataprovider ,no changes whatsoever
}
從不同地方呼叫時,(類)會發生變化,例如:Employee / Agency / etc 我們可以以某種方式做到這一點嗎?
我試過的:
public class DataStore<T>
{
public T data { get; set; }
}
public void ProcesAsyncReq(string className,string validatedInputs, string data1, string data2...)
{
DataStore<className> gclass1 = new DataStore<className>(); /*throws error that it is a
variable but is used as a type*/
gclass1.data=Newtonsoft.Json.JsonConvert.DeserializeObject(validatedInputs);
//logic here is to just pass the validatedInputs to the dataprovider ,no changes whatsoever
}
uj5u.com熱心網友回復:
您也可以使您的方法通用:
public void ProcesAsyncReq<T>(string validatedInputs)
{
DataStore<T> gclass1 = new DataStore<T>();
gclass1.data = JsonConvert.DeserializeObject<T>(validatedInputs);
...
}
你會這樣稱呼它:
ProcesAsyncReq<Employee>(myStringContainingSerializedEmployee);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/366527.html
上一篇:如何在Python2.7中使用時區從日期時間字串中獲取UNIX時間
下一篇:理解泛型以及如何
