Castle ProxyGenerator 接口代理的问题。

kissfu 2016-04-12 09:15:15
1.首先引用dllCastal.Core.dll
2.
public interface IStorageNode
{
bool IsDead { get; set; }
void Save(string message);
}
public interface IDao { }
public class BaseNode : IDao
{
protected string TestBase()
{
return "BaseNode";
}
}
public class StorageNode : BaseNode, IStorageNode
{
private string _name;
public StorageNode(string name)
{
this._name = name;
}
public bool IsDead { get; set; }
public void Save(string message)
{
Console.WriteLine(string.Format("\"{0}\" was saved to {1}", message, this._name));
}
}
public class DualNodeInterceptor : IInterceptor
{
private IStorageNode _slave;
public DualNodeInterceptor(IStorageNode slave)
{
this._slave = slave;
}
public void Intercept(IInvocation invocation)
{
IStorageNode master = invocation.InvocationTarget as IStorageNode;
if (master.IsDead)
{
IChangeProxyTarget cpt = invocation as IChangeProxyTarget;
//将被代理对象master更换为slave
cpt.ChangeProxyTarget(this._slave);
//测试中恢复master的状态,以便看到随后的调用仍然使用master这一效果
master.IsDead = false;
}
invocation.Proceed();
}
}
public class CallingLogInterceptor : IInterceptor
{
private int _indent = 0;
private void PreProceed(IInvocation invocation)
{
if (this._indent > 0)
Console.Write(" ".PadRight(this._indent * 4, ' '));
this._indent++;
Console.Write("Intercepting: " + invocation.Method.Name + "(");
if (invocation.Arguments != null && invocation.Arguments.Length > 0)
for (int i = 0; i < invocation.Arguments.Length; i++)
{
if (i != 0) Console.Write(", ");
Console.Write(invocation.Arguments[i] == null
? "null"
: invocation.Arguments[i].GetType() == typeof(string)
? "\"" + invocation.Arguments[i].ToString() + "\""
: invocation.Arguments[i].ToString());
}
Console.WriteLine(")");
}
private void PostProceed(IInvocation invocation)
{
this._indent--;
}
public void Intercept(IInvocation invocation)
{
this.PreProceed(invocation);
invocation.Proceed();
this.PostProceed(invocation);
}
}
3.测试代码
[TestMethod]
public void TestMethod1()
{
ProxyGenerator generator = new ProxyGenerator();
IDao daoInstance =new StorageNode("master");
IStorageNode temp = daoInstance as IStorageNode;//为什么temp不为null
IDao node1 = generator.CreateInterfaceProxyWithTarget(
daoInstance
, new DualNodeInterceptor(new StorageNode("slave"))
, new CallingLogInterceptor());
IStorageNode node = node1 as IStorageNode;//为什么node会是null
node.Save("my message"); //应该调用master对象
node.IsDead = true;
node.Save("my message"); //应该调用slave对象
node.Save("my message"); //应该调用master对象
}
问题是加红的地方。为什么node为null呢。
...全文
1184 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
TikYang 2016-07-06
  • 打赏
  • 举报
回复
[TestMethod] public void TestMethod1() { ProxyGenerator generator = new ProxyGenerator(); IDao daoInstance = new StorageNode("master"); IStorageNode daoInstance2 = new StorageNode("master"); IStorageNode temp = daoInstance as IStorageNode;//为什么temp不为null var node1 = generator.CreateInterfaceProxyWithTarget(typeof(IDao), new Type[] { typeof(IStorageNode)}, daoInstance , new DualNodeInterceptor(new StorageNode("slave")) , new CallingLogInterceptor()); IStorageNode node = node1 as IStorageNode;//为什么node会是null node.ToString(); node.Save("my message"); //应该调用master对象 node.IsDead = true; //下面运行会报错,原因见TestMethod12() //node.Save("my message"); //应该调用slave对象 //node.Save("my message"); //应该调用master对象 } [TestMethod] public void TestMethod12() { // interface proxy with interface target与interface proxy with target基本类似, // 但他提供了一个更改被代理对象(真实对象)的机会 // 只有在interface proxy with interface target的情况下IInterceptor的接口参数IInvocation对象 // 才实现了IChangeProxyTarget接口 ProxyGenerator generator = new ProxyGenerator(); IStorageNode daoInstance = new StorageNode("master"); IStorageNode temp = daoInstance as IStorageNode;//为什么temp不为null IStorageNode node = generator.CreateInterfaceProxyWithTargetInterface<IStorageNode>( daoInstance , new DualNodeInterceptor(new StorageNode("slave")) , new CallingLogInterceptor()); //StorageNode node = node1 as IStorageNode;//为什么node会是null node.Save("my message"); //应该调用master对象 node.IsDead = true; node.Save("my message"); //应该调用slave对象 node.Save("my message"); //应该调用master对象 }

13,347

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET技术前瞻
社区管理员
  • .NET技术前瞻社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧