www.pudn.com > Websharp2005.rar > AspectManagedAttribute.cs
using System;
using System.Runtime.Remoting.Proxies;
using System.Security.Permissions;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Contexts;
using System.Reflection;
namespace Websharp.Aspect
{
///
/// 自定义的Aspect代理特性,在这里,创建受Aspect管理的类的代理类。
/// 任何需要接受Aspect管理的类,都必须加上这个特性
///
/// 示例
///
/// [AspectManaged]
/// public class BusinessClass{}
///
///
///
///
/// [AspectManaged]
/// public class BusinessClass{}
///
///
[AttributeUsage(AttributeTargets.Class)]
[SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.Infrastructure)]
public class AspectManagedAttribute : ProxyAttribute
{
private bool aspectManaged;
///
/// 默认构造函数
///
public AspectManagedAttribute()
{
aspectManaged=true;
}
///
/// 构造函数
///
/// 指明该类是否接受Aspect管理
public AspectManagedAttribute(bool AspectManaged)
{
aspectManaged=AspectManaged;
}
///
/// 创建受Aspect管理的类的代理类
///
/// 要创建的类的类型
/// 受Aspect管理的类的代理类
public override MarshalByRefObject CreateInstance(Type serverType)
{
MarshalByRefObject mobj= base.CreateInstance(serverType);
if(aspectManaged)
{
RealProxy realProxy = new AspectProxy(serverType,mobj);
MarshalByRefObject retobj = realProxy.GetTransparentProxy() as MarshalByRefObject;
return retobj;
}
else
{
return mobj;
}
}
#region obsolesce code
/*
public override RealProxy CreateProxy(ObjRef objRef1,Type serverType,object serverObject,Context serverContext)
{
return new AspectProxy(serverType,serverObject as MarshalByRefObject);
AspectProxy myCustomProxy = new AspectProxy(serverType);
if(serverContext != null)
{
RealProxy.SetStubData(myCustomProxy,serverContext);
}
if((!serverType.IsMarshalByRef)&&(serverContext == null))
{
throw new RemotingException("Bad Type for CreateProxy");
}
return myCustomProxy;
}
*/
#endregion
}
}