Hello great community :)
I'm implementing my own network wrapper here and have few questions:
1)
The idea:
I want to do static "Network" class and refer to it as for example:
public class NetworkTest
{
public void StartPoint()
{
Network.LogIn(login, pass, OnLoginResponse);
}
public void onLoginResponse(ResponseObject obj)
{
//deal with response
}
}
public static class Network
{
private delegate ResponseObject RequestDelegate(string login, string pass, Action<ResponseObject> obj);
public void LogIn(string login, string pass, Action<ResponseObject> callBack)
{
RequestDelegate request = new RequestDelegate(new WebCommunicator().PostRequest);
request.BeginInvoke(login, pass, callBack, new AsyncCallback(AsyncResponse), null);
}
private static void AsyncResponse(IAsyncResult result)
{
AsyncResult res = (AsyncResult)result;
RequestDelegate request = (RequestDelegate)res.AsyncDelegate;
ResponseObject response = request.EndInvoke(result);
if(response != null)
{
response.CallBackMethod.Invoke(response);
}
}
}
but what if my "NetworkTest" implemented from template interface where i know for 100% that i would have methods like:
OnLoginResponse(ResponseObject obj)
OnRegisterResponse(ResponseObject obj)
and so on....
how can I delegate to them without passing them as argument to:
Network.LogIn(login, pass, OnLoginResponse);
so it looks like:
public class NetworkTest : INetInterface
{
public void Starter()
{
Network.LogIn(login, pass);
}
//implemented trough interface
public void OnLoginResponse(ResponseObject obj)
{
//handle response
}
}
public static class Network
{
private delegate ResponseObject RequestDelegate(string login, string pass);
public void LogIn(string login, string pass)
{
RequestDelegate request = new RequestDelegate(new WebCommunicator().PostRequest);
request.BeginInvoke(login, pass, new AsyncCallback(AsyncResponse), null);
}
private static void AsyncResponse(IAsyncResult result)
{
AsyncResult res = (AsyncResult)result;
RequestDelegate request = (RequestDelegate)res.AsyncDelegate;
ResponseObject response = request.EndInvoke(result);
if(response != null)
{
//how to fire NetworkTest.OnLoginREsponse(response) over here?
}
}
}
but call back still fall into OnLoginResponse as it is 100% because class made of pre-defained interface and such method is 100% there?
---------------------------------------------------------------------------------------
2)
Also 2nd question, how can I verifay in Network class that whoever called LogIn method was created from specific interface?
For example if I would forget to create my "NetworkTest" from template interface and would call Network.LogIn()
then "Network" calss would answer me that:
"you cant call LogIn() because you are not made of pre-defained interface, thu doesnt contain desired call back methods"
Thank you very much in advance :)