📄 agentproxy.cs
字号:
using System;
using System.Reflection;
namespace MobileAgents
{
/// <summary>
/// A sample implementation of an agent proxy class. A proxy class will
/// help protect object references.
/// </summary>
[Serializable]
public class AgentProxy
{
/// <summary>
/// A weak reference to the agent object instance.
/// </summary>
private WeakReference _agent = null;
/// <summary>
/// Keeps track of whether or not the agent is still at this location or not.
/// </summary>
private bool _agentLeft = false;
/// <summary>
/// Internal Ctor.
/// </summary>
/// <param name="agentToProxy">The Agent to wrap.</param>
internal AgentProxy(Agent agentToProxy)
{
_agent = new WeakReference(agentToProxy);
agentToProxy.AgentMoved += new EventHandler(agentToProxy_AgentMoved);
}
/// <summary>
/// Calls the wrapped Agent's Move method if the agent is still around.
/// </summary>
/// <param name="hostUrl">The url of the host to move to.</param>
public void Move(string hostUrl)
{
if ((_agent.IsAlive) && (!_agentLeft))
{
Agent agent = (Agent)_agent.Target;
agent.Move(hostUrl);
}
else
throw new Exception("The agent no longer exists here.");
}
/// <summary>
/// Invokes a member of the agent instance.
/// </summary>
/// <param name="memberName">The name of the member to invoke.</param>
/// <param name="memberArguments">The arguments for the member</param>
/// <returns>The return value (if any) resulting from invoking the member.</returns>
public object InvokeMethod(string methodName, params object[] methodArguments)
{
if ((_agent.IsAlive) && (!_agentLeft))
{
Agent a = (Agent)_agent.Target;
Type agentType = a.GetType();
object retVal = agentType.InvokeMember(methodName, BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, a, methodArguments);
return retVal;
}
else
throw new Exception("The agent no longer exists here.");
}
/// <summary>
/// Handler for the wrapped Agent's AgentMoved event. When the agent moves,
/// don't let others contact it!
/// </summary>
/// <param name="sender">Sender of event.</param>
/// <param name="e">Args of event.</param>
private void agentToProxy_AgentMoved(object sender, EventArgs e)
{
_agentLeft = true;
_agent.Target = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -