I wrote this method that takes in a generic parameter object. This method accepts different parameter objects with different properties. The idea is that based on the value of the "MethodName" property, I can construct a URI base on properties of that particular parameter object
I use reflection to get the value of the property named "MethodName." It's not a method, just a property name.
I use the value of the MethodName property in a case statement. This seems like an inferior way to accomplish my task. Especially as the number of MethodNames grow.
Below is the source with the case statement implementation. Any help would be appreciated.
public string ConstructBBAPIUri<T>(T parameters)
{
var methodName = "";
var uri = "";
var userId = "";
long timeStamp = 0;
var signature = "";
var sessionKey = "";
var newUserId = "";
if (parameters != null)
{
methodName = parameters.GetType().GetProperty("MethodName").GetValue(parameters, null).ToString();
switch (methodName)
{
case "user.login":
userId = parameters.GetType().GetProperty("UserId").GetValue(parameters, null).ToString();
timeStamp = Convert.ToInt64(parameters.GetType().GetProperty("TimeStamp").GetValue(parameters, null));
signature = GenerateBunchBallSignature(userId);
uri = "method=" + methodName + "&apiKey=" + apiKey + "&userid=" + userId + "&ts=" +
timeStamp + "&sig=" + signature;
break;
case "user.modifyUserId":
//We shouldn't need the session key if user.login is being called first
userId = parameters.GetType().GetProperty("UserId").GetValue(parameters, null).ToString();
newUserId = parameters.GetType().GetProperty("NewUserId").GetValue(parameters, null).ToString();
uri = "method=" + methodName + "&sessionKey=" + sessionKey + "&oldUserId=" + userId +
"&newUserId=" + newUserId;
break;
}
}
return uri;
}