我有一个APIRequest类,我需要一个isInBackground变量的默认值。
APIRequest类简化:
public class APIRequest
{
public string url;
public bool isInBackground = true;
}
当我这样做时,isInBackground的值总是被忽略并设置为默认值:
APIRequest rq = new APIRequest{
url = "/user/suggest/" + pseudo,
isInBackground = false
};
然后我记录该值并获得“真实”,但是url是正确的值......
将其设置为“假”的唯一方法是是做
rq.isInBackground = false;
然后该值确实为“假”'
所以我的问题是它为什么会这样?是因为isInBackground有默认值吗?
public class APITest {
public string url;
public bool background = true;
}
public class TestClass : MonoBehaviour {
// Use this for initialization
void Start () {
APITest t = new APITest{
url = "coucou",
background = false
};
Debug.Log(" url: " + t.url + " background: " + t.background);
}
}
编辑:尝试将默认值设置为' False'并将变量设置为' True'这个问题没有发生......
编辑以显示完整代码:
private void ShowPseudoSuggestionsFor(string pseudo){
Debug.Log("Get suggestions");
RemoteRequest r = RemoteRequest.GetNewRequest("PseudoSuggestions");
APIRequest rq = new APIRequest{
url = "/user/suggest/" + pseudo,
successCallback = OnSuggestionsLoaded,
successCallbackArgs = new Hashtable{
{ "pseudo", pseudo }
},
isInBackground = false
};
//rq.isInBackground = false;
Debug.Log("isInBackground : " + rq.isInBackground);
r.SetRequest(rq);
Debug.Log("isInBackground : " + rq.isInBackground);
r.SendRequest();
}
完整的APIRequest课程:
public class APIRequest
{
public string url; /*!< Url you wish to send you request at */
public HTTPMethod method = HTTPMethod.GET; /*!< HTTP method (GET, POST, PUT, DELETE) */
public Dictionary<string, string> headers = new Dictionary<string, string>(); /*!< Headers, add one with headers.Add(<headerName>, <headerValue>) */
public byte[] payload; /*!< If you need to send POST or PUT data create a WWWForm, add the fields you need in it and then set payload to be <yourForm>.data */
public bool resendOnFailure = false; /*!< Should we resend the request if it fails ? */
public bool resendOnTimeout = false; /*!< Should we resend the request if it times out ? */
public bool isInBackground = true; /*!< Should we send request start and completed events */
public bool askBeforeResending = false; /*!< Should we ask user before resending request ? */
public Rect askingPopupRect; /*!< Size of the popup that will ask user to resend request */
public string askingPopupTitle; /*!< Title for resend request popup */
public string askingPopupMessage; /*!< Message for resend request popup */
public GUI.WindowFunction drawAskingPopup; /*!< Function responsible for drawing the resend request popup */
public float timeout = 10f; /*!< Time until the request is considered a failure */
public int maxAttempts = 1; /*!< Maximum number of attempts before which request will be resent, upon reaching it, no more attempts to resend will be made */
public RequestCallback successCallback; /*!< Function called when server responded with a status code 2XX (success) */
public RequestCallback failureCallback; /*!< Any other status code than 2XX will trigger this function (failure) */
public Hashtable failureCallbackArgs; /*!< This hashtable will be passed along to the 'successCallback' function as a mean to provide it with arguments */
public Hashtable successCallbackArgs; /*!< This hashtable will be passed along to the 'failureCallback' function as a mean to provide it with arguments */
/** Returns a string representation of the request that is human readable
* \return A string describing the request parameters
*/
public override string ToString(){
string result = "Request to url: " + url + " method: " + method.ToString () + "\nHeaders:\n";
foreach(KeyValuePair<string, string> kvp in headers){
result += kvp.Key + " -> " + kvp.Value + "\n";
}
result += "Params :\n";
result += "resendOnFailure: " + resendOnFailure + "\n";
result += "resendOnTimeout: " + resendOnTimeout + "\n";
result += "askBeforeResending: " + askBeforeResending + "\n";
result += "timeout: " + timeout + "\n";
result += "maxAttempts: " + maxAttempts + "\n";
result += "isInBackground: " + isInBackground + "\n";
return result;
}
}