如提:用C#实现AOP中的类属性是根据什么设的
如下是代码
请问[InterceptProxy]
是固定的吗?如果可以换成别的,是根据什么设的using System;
using System.Runtime.Remoting ;
using System.Runtime.Remoting.Services ;
using System.Runtime.Remoting.Activation ;
using System.Runtime.Remoting.Proxies ;
using System.Runtime.Remoting.Messaging ;/*
  本程序说明了截获是如何进行的,以及真实代理和透明代理之间的关系及交互。
*/
namespace Intercept_example
{
class Class1
{  
[STAThread]
static void Main(string[] args)
{
Example exa = new Example("sky" ) ;
exa.say_hello() ;
}

 
//定义一个真实代理,用于实现一个特定的预处理和后处理
public class InterceptProx :  RealProxy
{
private readonly MarshalByRefObject target ;
  
public InterceptProx(MarshalByRefObject obj ,Type type) :base(type)
{
this.target = obj ;
}  
public override IMessage Invoke(IMessage msg)
{
IMethodCallMessage call = (IMethodCallMessage)msg ;
   
//如果触发的是构造函数,此时target的构建还未开始
IConstructionCallMessage ctor = call as IConstructionCallMessage ;
if(ctor != null)
{
System.Console.WriteLine("转发构造函数调用!") ; RealProxy default_proxy = RemotingServices.GetRealProxy(this.target) ; default_proxy.InitializeServerObject(ctor) ;
MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy() ; return EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor,tp);
    
} System.Console.WriteLine("预处理中......") ;
System.Threading.Thread.Sleep(5000) ;
   
IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.target ,call) ;
   
System.Console.WriteLine("后处理中......") ;
System.Threading.Thread.Sleep(5000) ;
System.Console.WriteLine("后处理结束!") ; return result_msg ;  }
}
  
  //定义一个特性,该特性可以将上面的真实代理与运用该特性的class联系起来
[AttributeUsage(AttributeTargets.Class)]
public class InterceptProxyAttribute : ProxyAttribute
{
//得到透明代理
public override MarshalByRefObject CreateInstance(Type serverType)
{
//未初始化的实例
MarshalByRefObject target =  base.CreateInstance (serverType); InterceptProx rp = new InterceptProx(target ,serverType) ; return (MarshalByRefObject)rp.GetTransparentProxy() ;
}
}
// [AttributeUsage(AttributeTargets.Class)]
// public class aaaa : ProxyAttribute
// {
// //得到透明代理
// public override MarshalByRefObject CreateInstance(Type serverType)
// {
// //未初始化的实例
// MarshalByRefObject target =  base.CreateInstance (serverType);
//
// InterceptProx rp = new InterceptProx(target ,serverType) ;
//
// return (MarshalByRefObject)rp.GetTransparentProxy() ;
// }
// } [InterceptProxy]
public class Example : ContextBoundObject//放到特定的上下文中,该上下文外部才会得到该对象的透明代理
{
private string name ;
public Example(string a)
{
this.name = a ;
} public void say_hello()
{
Console.WriteLine("hello ! " + name ) ;
}
} [InterceptProxy]
public class eddy
{
public eddy()
{}
public void sayhello()
{
Console.WriteLine("hello world");
}
}
}

解决方案 »

  1.   

    as far as I know, Attribute is static, once set, you cannot change itit is a big topic, read Don Box's Essential .NET Volume 1:
    http://www.china-pub.com/computers/common/info.asp?id=14700also seeAspect-Oriented Programming Enables Better Code Encapsulation and Reuse
    http://msdn.microsoft.com/msdnmag/issues/02/03/AOP/
      

  2.   

    [InterceptProxy]其实是一个卧底......
    是下面这个类的一个实例
    [AttributeUsage(AttributeTargets.Class)]
    public class InterceptProxyAttribute : ProxyAttribute
    {...}Attribute:
    公共语言运行库允许您添加类似关键字的描述性声明(称为属性)来批注编程元素,如类型、字段、方法和属性。属性与 Microsoft .NET Framework 文件的元数据一起保存,并且可用于向运行库描述代码或影响应用程序的运行库行为。.NET Framework 提供了许多有用的属性,但您也可以设计和部署自己的属性。TransparentProxy(透明代理):
    客户端在跨任何类型的远程处理边界使用对象时,对对象使用的实际上是透明代理。透明代理使人以为实际对象驻留在客户端空间中。透明代理实现这一点的方法是:使用远程处理基础结构将对其进行的调用转发给真实对象。
    透明代理本身由 RealProxy 类型的托管运行时类的实例收容。RealProxy 实现从透明代理转发操作所需的部分功能。代理对象继承托管对象(例如垃圾回收、对字段和方法的支持)的关联语义,可以将其进行扩展以形成新类。这样,该代理具有双重性质,一方面,它需要充当与远程对象(透明代理)相同的类的对象;另一方面,它本身是托管对象。http://www.microsoft.com/china/community/program/originalarticles/TechDoc/proxymod.mspx
    http://www.microsoft.com/china/MSDN/library/windev/COMponentdev/AspectOrientedProgrammingEnablesBetterCodeEncapsulationandReuse.mspx
    你可以跟踪一下,看看代码是怎么跑的,然后再想想他为什么要这么跑,然后查查资料,然后再想,再查......
      

  3.   

    用.net类库实现AOP麻烦了点吧~~
    向楼主推荐一个轻量级AOP框架Aspect#,不需改变现有代码(除了把类改为从接口继承外)!.
    分析文章请见:http://www.narchitecture.net/default.aspx