Teddy's Knowledge Base

Teddy's Aspect Weaver Version 0.3 with Great Updating and Fixing, Especially the Implementing of Getting Runtime Method Context Info and Method Arguments in MSIL Level

[More about Teddy's Aspect Weaver]

Teddy's Aspect Weaver Version 0.3 [Updated: 2005/09/27]

Changes:
  - Add the geting context info and getting context arguments ability
  - Fix some internal XPath bugs
  - Fix BeforeConstructorCall/BeforeMethodCall bugs
  - Fix the ILML Library parsing method parameter name bug
  - Add the object instance in aspect local stack ability, which means you can new any object in aspect code only if the type of you object is defined or can be referenced in the base assembly
  - Move assembly AspectWeaver.Attributes code to assembly AspectWeaver.Aspects

Sample of Getting Runtime Method Context Info and Method Arguments

Configuration

<?xml version="1.0" encoding="utf-8" ?> 
<Configuration logFile="LogWeaving.xml" cleanTempFiles="false">
    
<BaseAssembly>..\TestLib\bin\Debug\TestLib.dll</BaseAssembly>
    
<OutputAssembly>..\TestLib\bin\Debug\TestLib_output.dll</OutputAssembly>
    
<AspectAssemblies>
        
<AspectAssembly uniqueName="TestAspectLib.dll" path="bin\Debug\TestAspectLib.dll" />
    
</AspectAssemblies>
    
<AdviceFiles>
        
<AdviceFile>Advice-Test.xml</AdviceFile>
    
</AdviceFiles>
</Configuration>

TestClass.cs

using System;

namespace TestLib
{
    
public class TestClass
    
{
        
public void MethodToBeTestedGetContextInfoAndGetArgumments(object p1, string p2, int p3)
        
{
        }


        
public void MethodToTestGetContextInfoAndGetArgumments()
        
{
            MethodToBeTestedGetContextInfoAndGetArgumments(
"p1""p2", 3);
        }

    }

}

TestAspectClass.cs

using System;
using System.Reflection;
using AspectWeaver.Aspects;

namespace TestAspectLib
{
    
public class TestAspectClass : Aspect
    
{
        [InlineAtStart(
"//Method[@name='MethodToBeTestedGetContextInfoAndGetArgumments']")]
        
public void CodeToTestGetContextInfoAndGetArgumments()
        
{
            MethodBase contextInfo 
= GetContextInfo();
            Console.Write(contextInfo.ReflectedType.FullName 
+ "::" + contextInfo.Name + "\n\n");
            
object[] args = GetArguments();
            Console.Write(
string.Format("Arguments: {0}, {1}, {2}\n\n", args));
        }

    }

}

After Weaving Code (decompiled)

  public class TestClass
  
{
    
public void MethodToBeTestedGetContextInfoAndGetArgumments(object p1, string p2, int p3)
    
{
      MethodBase methodBase 
= base.GetType().GetMethod("MethodToBeTestedGetContextInfoAndGetArgumments");
      Console.Write(String.Concat(methodBase.ReflectedType.FullName, 
"::", methodBase.Name, "\n\n"));
      
object[] locals = new object[]{p1, p2, p3};
      Console.Write(String.Format(
"Arguments: {0}, {1}, {2}\n\n", locals));
    }


    
public void MethodToTestGetContextInfoAndGetArgumments()
    
{
      MethodToBeTestedGetContextInfoAndGetArgumments("p1""p2", 3);
    }

  }

Description

In the sample, we can see, if you call the GetContextInfo() and GetArguments() from aspect classes which inherited from AspectWeaver.Aspects.Aspect which defined the two methods, the weaved code be translated to local reflection code, no additional dependence. But it is interesting that in fact, there are not any code in the two methods' body at all. Do you know why? :^)

Download Source Code

AspectWeaver0.5.zip

 

posted on 2005-09-27 10:43 Teddy's Knowledge Base 阅读(1546) 评论(4)  编辑 收藏 网摘 所属分类: AOSD

评论

#1楼  2005-09-27 11:52 idior      

good job. context problem seems to be sloved.

if we can get the object instance in the aspect methd whose method have been intercepted ?
  回复  引用  查看    

#2楼 [楼主] 2005-09-27 12:01 Teddy's Knowledge Base      

@idior:

Methods to be intercepted are specified by the targetXPath or the custom atrribute arg, here "//Method[@name='MethodToBeTestedGetContextInfoAndGetArgumments']" is a simple example, in real use situation, it may be like "//Type[@name="some class"]/Method[@name='some method']"; use can use any valid xpath to sepcify the target method.

The GetContextInfo() and GetArguments() will get the runtime method context, which means for InlineAtStart/InlineBeforeReturn we get the target method's context, and for XXXMethodCall/XXXConstructorCall we get the method calling the target method's context. All the time, they return the runtime method's context!
  回复  引用  查看    

#3楼  2005-09-27 14:06 idior      

i still have some worrys.
1. if Class A have a instance "a" a.Foo(); method has been intercepted.
and then in the aspect method can we get the dynamic info of "a"
2. if the aspect method is "after" type in which can we get the arguments that have been already modified by the original method.
  回复  引用  查看    

#4楼 [楼主] 2005-09-27 14:11 Teddy's Knowledge Base      

You worries are not problems at all. Because static weaving is done before code running, which means at runtime, the a.Foo() is already an intercepted a.Foo()., and the a's info is the weaved class A's runtime instance a's info. No so called original method at runtime at all.

And you can easily create some example to test it.
  回复  引用  查看    


发表评论



姓名 [登录] [注册] 
主页
Email (仅博主可见) 
验证码 *  验证码看不清,换一张
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论   新用户注册   返回页首      

导航: 网站首页 社区 新闻 博问 闪存 网摘 招聘 .NET频道 知识库 找找看 Google站内搜索



China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
China-Pub 计算机绝版图书按需印刷服务

相关文章:

相关链接: