注意,这里我们实现了IXmlSerialization接口,以实现自定义的反序列化操作,还应注意的是在反串行的过程中,这里我们使用了一个自已实现的深拷贝DeepCopy
public class AspectCollection:CollectionBase,IXmlSerializable
{
public AspectCollection()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void Add(AspectItem ai)
{
this.InnerList.Add(ai);
}
public new AspectItem this[int index]
{
get
{
return this.InnerList[index] as AspectItem;
}
set
{
this.InnerList[index]=value;
}
}
IXmlSerializable 成员
这里我使用了.net内置的序列化功能{
public AspectCollection()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void Add(AspectItem ai)
{
this.InnerList.Add(ai);
}
public new AspectItem this[int index]
{
get
{
return this.InnerList[index] as AspectItem;
}
set
{
this.InnerList[index]=value;
}
}
IXmlSerializable 成员
using System;
using System.Xml.Serialization;
namespace Langzhi.Aspect
{
/// <summary>
/// AspectItem 的摘要说明。
/// </summary>
[Serializable()]
public class AspectItem
{
public AspectItem()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private string m_Type;
private string m_DeployModel;
private string m_PointCutType;
private string m_ActionPosition;
private RuleCollection m_RuleCollection;
// private string m_ID;
private string m_AssemblyName;
private string m_ClassName;
[XmlAttribute("type")]
public string Type
{
get
{
return this.m_Type;
}
set
{
this.m_Type=value;
}
}
[XmlAttribute("deploy-model")]
public string DeployModel
{
get
{
return this.m_DeployModel;
}
set
{
this.m_DeployModel=value;
}
}
[XmlAttribute("pointcut-type")]
public string PointCutType
{
get
{
return this.m_PointCutType;
}
set
{
this.m_PointCutType=value;
}
}
[XmlAttribute("action-position")]
public string ActionPosition
{
get
{
return this.m_ActionPosition;
}
set
{
this.m_ActionPosition=value;
}
}
// [XmlAttribute("id")]
// public string ID
// {
// get
// {
// return this.m_ID;
// }
// set
// {
// this.m_ID = value;
// }
//
// }
[NonSerialized()]
public string ID;
[NonSerialized()]
public string AssemblyName;
[NonSerialized()]
public string ClassName;
[NonSerialized()]
public IAspect SingletonAspect;
public RuleCollection Rules
{
get
{
return this.m_RuleCollection;
}
set
{
this.m_RuleCollection=value;
}
}
}
}
using System.Xml.Serialization;
namespace Langzhi.Aspect
{
/// <summary>
/// AspectItem 的摘要说明。
/// </summary>
[Serializable()]
public class AspectItem
{
public AspectItem()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private string m_Type;
private string m_DeployModel;
private string m_PointCutType;
private string m_ActionPosition;
private RuleCollection m_RuleCollection;
// private string m_ID;
private string m_AssemblyName;
private string m_ClassName;
[XmlAttribute("type")]
public string Type
{
get
{
return this.m_Type;
}
set
{
this.m_Type=value;
}
}
[XmlAttribute("deploy-model")]
public string DeployModel
{
get
{
return this.m_DeployModel;
}
set
{
this.m_DeployModel=value;
}
}
[XmlAttribute("pointcut-type")]
public string PointCutType
{
get
{
return this.m_PointCutType;
}
set
{
this.m_PointCutType=value;
}
}
[XmlAttribute("action-position")]
public string ActionPosition
{
get
{
return this.m_ActionPosition;
}
set
{
this.m_ActionPosition=value;
}
}
// [XmlAttribute("id")]
// public string ID
// {
// get
// {
// return this.m_ID;
// }
// set
// {
// this.m_ID = value;
// }
//
// }
[NonSerialized()]
public string ID;
[NonSerialized()]
public string AssemblyName;
[NonSerialized()]
public string ClassName;
[NonSerialized()]
public IAspect SingletonAspect;
public RuleCollection Rules
{
get
{
return this.m_RuleCollection;
}
set
{
this.m_RuleCollection=value;
}
}
}
}
public class RuleCollection:CollectionBase//,IXmlSerializable//此处可以不用实现IXmlSerializable接口
{
static readonly log4net.ILog log=log4net.LogManager.GetLogger(typeof(RuleCollection));
public RuleCollection()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void Add(RuleItem ari)
{
this.InnerList.Add(ari);
}
public RuleItem this[int index]
{
get
{
return (RuleItem)this.List[index];
}
set
{
this.InnerList[index]=value;
}
}
IXmlSerializable 成员
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public RuleCollection FindCollectionByType(string type)
{
type=type.Trim().ToLower();
RuleCollection rc=new RuleCollection();
foreach(RuleItem ri in this.InnerList)
{
if (ri.type.ToLower()==type)
{
rc.Add(ri);
}
}
if (rc.Count!=0)
{
return rc;
}
else
{
return null;
}
}
public bool IsMatch(IMethodMessage mmsg,AspectItem aspectInfo)
{
// if (this.InnerList==null)
// {
// return false;
// }
foreach (RuleItem ri in this.InnerList)
{
//是否匹配类和方法
string[] match= ri.match.Split(',');
string classNamePattern= string.Format("^{0}$",match[0]);
string methodNamePattern = string.Format("^{0}$",match[1]);
// log.Debug(string.Format("classNamePattern:{0}", classNamePattern));
// log.Debug(string.Format("methodNamePattern:{0}", methodNamePattern));
// log.Debug(mmsg.MethodBase.ReflectedType.FullName);
// log.Debug(mmsg.MethodName);
// log.Debug(mmsg.Properties["__TypeName"].ToString().Split(',')[0].ToString());
// log.Debug(mmsg.Properties["__MethodName"].ToString());
// log.Debug(mmsg.MethodBase.ReflectedType.Name);
if (Regex.IsMatch(mmsg.MethodBase.ReflectedType.FullName,classNamePattern))
{
log.Debug(string.Format("匹配类:{0}", classNamePattern));
if (mmsg.MethodBase.IsConstructor && aspectInfo.PointCutType.IndexOf("Construction")>-1)
{
log.Debug("construction");
log.Debug(mmsg.MethodName);
return true;
}
if ((aspectInfo.PointCutType.IndexOf("Method")>-1 || aspectInfo.PointCutType.IndexOf("Property")>-1) && Regex.IsMatch(mmsg.MethodName,methodNamePattern)==true)
{
log.Debug("method or property");
log.Debug(mmsg.MethodName);
return true;
}
}
}
return false;
}
public static byte[] Serialize(RuleCollection rc)
{
BinaryFormatter bf=new BinaryFormatter();
MemoryStream ms=new MemoryStream();
byte[] b;
bf.Serialize(ms, rc);
ms.Position = 0;
b=new byte[ms.Length];
ms.Read(b, 0, b.Length);
ms.Close();
return b;
}
public static RuleCollection Deserizlize(byte[] b)
{
if (b.Length==0)
{
return null;
}
// try
// {
BinaryFormatter bf=new BinaryFormatter();
MemoryStream ms=new MemoryStream();
ms.Write(b, 0, b.Length);
ms.Position = 0;
RuleCollection rc = (RuleCollection) bf.Deserialize(ms);
ms.Close();
return rc;
// }
// catch (System.Exception e)
// {
//
// }
}
public RuleCollection DeepCopy()
{
BinaryFormatter bf=new BinaryFormatter();
MemoryStream ms=new MemoryStream();
byte[] b;
bf.Serialize(ms, this);
ms.Position = 0;
RuleCollection rc = (RuleCollection) bf.Deserialize(ms);
ms.Close();
return rc;
}
}
{
static readonly log4net.ILog log=log4net.LogManager.GetLogger(typeof(RuleCollection));
public RuleCollection()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void Add(RuleItem ari)
{
this.InnerList.Add(ari);
}
public RuleItem this[int index]
{
get
{
return (RuleItem)this.List[index];
}
set
{
this.InnerList[index]=value;
}
}
IXmlSerializable 成员
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public RuleCollection FindCollectionByType(string type)
{
type=type.Trim().ToLower();
RuleCollection rc=new RuleCollection();
foreach(RuleItem ri in this.InnerList)
{
if (ri.type.ToLower()==type)
{
rc.Add(ri);
}
}
if (rc.Count!=0)
{
return rc;
}
else
{
return null;
}
}
public bool IsMatch(IMethodMessage mmsg,AspectItem aspectInfo)
{
// if (this.InnerList==null)
// {
// return false;
// }
foreach (RuleItem ri in this.InnerList)
{
//是否匹配类和方法
string[] match= ri.match.Split(',');
string classNamePattern= string.Format("^{0}$",match[0]);
string methodNamePattern = string.Format("^{0}$",match[1]);
// log.Debug(string.Format("classNamePattern:{0}", classNamePattern));
// log.Debug(string.Format("methodNamePattern:{0}", methodNamePattern));
// log.Debug(mmsg.MethodBase.ReflectedType.FullName);
// log.Debug(mmsg.MethodName);
// log.Debug(mmsg.Properties["__TypeName"].ToString().Split(',')[0].ToString());
// log.Debug(mmsg.Properties["__MethodName"].ToString());
// log.Debug(mmsg.MethodBase.ReflectedType.Name);
if (Regex.IsMatch(mmsg.MethodBase.ReflectedType.FullName,classNamePattern))
{
log.Debug(string.Format("匹配类:{0}", classNamePattern));
if (mmsg.MethodBase.IsConstructor && aspectInfo.PointCutType.IndexOf("Construction")>-1)
{
log.Debug("construction");
log.Debug(mmsg.MethodName);
return true;
}
if ((aspectInfo.PointCutType.IndexOf("Method")>-1 || aspectInfo.PointCutType.IndexOf("Property")>-1) && Regex.IsMatch(mmsg.MethodName,methodNamePattern)==true)
{
log.Debug("method or property");
log.Debug(mmsg.MethodName);
return true;
}
}
}
return false;
}
public static byte[] Serialize(RuleCollection rc)
{
BinaryFormatter bf=new BinaryFormatter();
MemoryStream ms=new MemoryStream();
byte[] b;
bf.Serialize(ms, rc);
ms.Position = 0;
b=new byte[ms.Length];
ms.Read(b, 0, b.Length);
ms.Close();
return b;
}
public static RuleCollection Deserizlize(byte[] b)
{
if (b.Length==0)
{
return null;
}
// try
// {
BinaryFormatter bf=new BinaryFormatter();
MemoryStream ms=new MemoryStream();
ms.Write(b, 0, b.Length);
ms.Position = 0;
RuleCollection rc = (RuleCollection) bf.Deserialize(ms);
ms.Close();
return rc;
// }
// catch (System.Exception e)
// {
//
// }
}
public RuleCollection DeepCopy()
{
BinaryFormatter bf=new BinaryFormatter();
MemoryStream ms=new MemoryStream();
byte[] b;
bf.Serialize(ms, this);
ms.Position = 0;
RuleCollection rc = (RuleCollection) bf.Deserialize(ms);
ms.Close();
return rc;
}
}
______________________________
朗志工作室:承接北京地区网站类项目