你好社区:)
我在这里实现了自己的网络包装器并且几乎没有问题:
1)
这个想法:
我想做静态的“网络”类,并参考它,例如:
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);
}
}
}
但是,如果我的“NetworkTest”从我知道100%的模板界面实现,我会有如下方法:
OnLoginResponse(ResponseObject obj)
OnRegisterResponse(ResponseObject obj)
依旧......
我如何委托给他们而不将他们作为参数传递给:
Network.LogIn(login, pass, OnLoginResponse);
所以它看起来像:
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?
}
}
}
但是回调仍然属于OnLoginResponse,因为它是100%,因为由预先褪色的接口制成的类和这样的方法是100%吗?
-------------------------------------------------- -------------------------------------
的 2)
还有第二个问题,我怎样才能在Network类中验证调用LogIn方法的人是从特定接口创建的?
例如,如果我忘记从模板界面创建我的“NetworkTest”并调用Network.LogIn(),那么“网络”calss会回答我:“你不能调用LogIn( )因为你不是由预先伪装的界面构成的,所以不包含所需的回调方法“
非常感谢您提前:))