WebSharp Aspect改进(续2)

    接着上次在 《朗志轻量级项目管理解决方案》中对Aspect的改进,
注意,这里我们实现了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 成员#region IXmlSerializable 成员

        
public void WriteXml(XmlWriter writer)
        
{
            
// TODO:  添加 AspectCollection.WriteXml 实现
            foreach(AspectItem ai in this.InnerList)
            
{
                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();
             }

        }


        
public System.Xml.Schema.XmlSchema GetSchema()
        
{
            
// TODO:  添加 AspectCollection.GetSchema 实现
            return null;
        }


        
public void ReadXml(XmlReader reader)
        
{
            
// TODO:  添加 AspectCollection.ReadXml 实现
            base.Clear();
            reader.MoveToContent();
            AspectCollection ac
=new AspectCollection();
            RuleCollection rc
=new RuleCollection();
            AspectItem ai
=new AspectItem();
            
while(reader.Read())
            
{
                
if (reader.IsStartElement("Aspect"))
                
{
                    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;
                }


                
if (reader.IsStartElement("Rule"))
                
{
                    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();
                }


            }
        
        }
这里我使用了.net内置的序列化功能
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;
            }

        }


    }

}


     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 成员#region IXmlSerializable 成员

        
public void WriteXml(XmlWriter writer)
        
{
            
// TODO:  添加 AuthenticationRuleCollection.WriteXml 实现
            for (int i=0;i<this.InnerList.Count;i++)
            
{
                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();
            }

        }


        
public System.Xml.Schema.XmlSchema GetSchema()
        
{
            
// TODO:  添加 AuthenticationRuleCollection.GetSchema 实现
            return null;
        }


        
public void ReadXml(XmlReader reader)
        
{
            
// TODO:  添加 AuthenticationRuleCollection.ReadXml 实现
            base.Clear();
            reader.MoveToContent();
            
while(reader.Read())
            
{
                
if (reader.IsStartElement("Rule"))
                
{
                    RuleItem ari
=new RuleItem();
                    reader.MoveToAttribute(
"type");
                    ari.type
=reader.Value;
                    reader.MoveToAttribute(
"match");
                    ari.match
=reader.Value;
                    
this.Add(ari);
                    reader.MoveToElement();
                }


            }

        }


        
#endregion

        
/**//// <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;
        }

    }


______________________________
朗志工作室:承接北京地区网站类项目
posted on 2007-11-19 21:39  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2007/11/19/964833.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值