接着上次在
《朗志轻量级项目管理解决方案》中对Aspect的改进,
注意,这里我们实现了IXmlSerialization接口,以实现自定义的反序列化操作,还应注意的是在反串行的过程中,这里我们使用了一个自已实现的深拷贝DeepCopy
public class AspectCollection:CollectionBase,IXmlSerializable
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public AspectCollection()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void Add(AspectItem ai)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.InnerList.Add(ai);
}
public new AspectItem this[int index]
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.InnerList[index] as AspectItem;
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.InnerList[index]=value;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
IXmlSerializable 成员#region IXmlSerializable 成员
![](/Images/OutliningIndicators/InBlock.gif)
public void WriteXml(XmlWriter writer)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// TODO: 添加 AspectCollection.WriteXml 实现
foreach(AspectItem ai in this.InnerList)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
writer.WriteStartElement("Aspect");
writer.WriteAttributeString("","type","",ai.Type);
writer.WriteAttributeString("","deploy-model","",ai.DeployModel);
writer.WriteAttributeString("","pointcut-type","",ai.PointCutType);
writer.WriteAttributeString("","action-position","",ai.ActionPosition);
ai.Rules.WriteXml(writer);
writer.WriteEndElement();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public System.Xml.Schema.XmlSchema GetSchema()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// TODO: 添加 AspectCollection.GetSchema 实现
return null;
}
![](/Images/OutliningIndicators/InBlock.gif)
public void ReadXml(XmlReader reader)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// TODO: 添加 AspectCollection.ReadXml 实现
base.Clear();
reader.MoveToContent();
AspectCollection ac=new AspectCollection();
RuleCollection rc=new RuleCollection();
AspectItem ai=new AspectItem();
while(reader.Read())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (reader.IsStartElement("Aspect"))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ai=new AspectItem();
rc.Clear();
reader.MoveToAttribute("type");
ai.Type=reader.Value;
reader.MoveToAttribute("deploy-model");
ai.DeployModel=reader.Value;
reader.MoveToAttribute("pointcut-type");
ai.PointCutType=reader.Value;
reader.MoveToAttribute("action-position");
ai.ActionPosition=reader.Value;
this.Add(ai);
continue;
}
![](/Images/OutliningIndicators/InBlock.gif)
if (reader.IsStartElement("Rule"))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
RuleItem ri=new RuleItem();
reader.MoveToAttribute("type");
ri.type=reader.Value;
reader.MoveToAttribute("match");
ri.match=reader.Value;
rc.Add(ri);
ai.Rules=rc.DeepCopy();
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
这里我使用了.net内置的序列化功能
using System;
using System.Xml.Serialization;
namespace Langzhi.Aspect
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// AspectItem 的摘要说明。
/// </summary>
[Serializable()]
public class AspectItem
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public AspectItem()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// 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
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.m_Type;
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_Type=value;
}
}
[XmlAttribute("deploy-model")]
public string DeployModel
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.m_DeployModel;
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_DeployModel=value;
}
}
[XmlAttribute("pointcut-type")]
public string PointCutType
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.m_PointCutType;
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_PointCutType=value;
}
}
[XmlAttribute("action-position")]
public string ActionPosition
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.m_ActionPosition;
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this.m_RuleCollection;
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.m_RuleCollection=value;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
![](/Images/OutliningIndicators/None.gif)
public class RuleCollection:CollectionBase//,IXmlSerializable//此处可以不用实现IXmlSerializable接口
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
static readonly log4net.ILog log=log4net.LogManager.GetLogger(typeof(RuleCollection));
public RuleCollection()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void Add(RuleItem ari)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.InnerList.Add(ari);
}
public RuleItem this[int index]
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (RuleItem)this.List[index];
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.InnerList[index]=value;
}
}
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
IXmlSerializable 成员#region IXmlSerializable 成员
![](/Images/OutliningIndicators/InBlock.gif)
public void WriteXml(XmlWriter writer)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// TODO: 添加 AuthenticationRuleCollection.WriteXml 实现
for (int i=0;i<this.InnerList.Count;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
writer.WriteStartElement("Rule");
writer.WriteStartAttribute("type","");
writer.WriteString(this[i].type);
writer.WriteEndAttribute();
writer.WriteStartAttribute("match","");
writer.WriteString(this[i].match);
writer.WriteEndAttribute();
writer.WriteEndElement();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public System.Xml.Schema.XmlSchema GetSchema()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// TODO: 添加 AuthenticationRuleCollection.GetSchema 实现
return null;
}
![](/Images/OutliningIndicators/InBlock.gif)
public void ReadXml(XmlReader reader)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// TODO: 添加 AuthenticationRuleCollection.ReadXml 实现
base.Clear();
reader.MoveToContent();
while(reader.Read())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (reader.IsStartElement("Rule"))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
RuleItem ari=new RuleItem();
reader.MoveToAttribute("type");
ari.type=reader.Value;
reader.MoveToAttribute("match");
ari.match=reader.Value;
this.Add(ari);
reader.MoveToElement();
}
![](/Images/OutliningIndicators/InBlock.gif)
}
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public RuleCollection FindCollectionByType(string type)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
type=type.Trim().ToLower();
RuleCollection rc=new RuleCollection();
foreach(RuleItem ri in this.InnerList)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ri.type.ToLower()==type)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rc.Add(ri);
}
}
if (rc.Count!=0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return rc;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public bool IsMatch(IMethodMessage mmsg,AspectItem aspectInfo)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// if (this.InnerList==null)
// {
// return false;
// }
foreach (RuleItem ri in this.InnerList)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//是否匹配类和方法
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))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
log.Debug(string.Format("匹配类:{0}", classNamePattern));
if (mmsg.MethodBase.IsConstructor && aspectInfo.PointCutType.IndexOf("Construction")>-1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
log.Debug("method or property");
log.Debug(mmsg.MethodName);
return true;
}
}
}
return false;
}
public static byte[] Serialize(RuleCollection rc)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (b.Length==0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
// {
//
// }
![](/Images/OutliningIndicators/InBlock.gif)
}
public RuleCollection DeepCopy()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
}
}
______________________________
朗志工作室:承接北京地区网站类项目