使用PostSharp在c#中构造函数上应用aspect

使用PostSharp在c#中构造函数上应用aspect,c#,postsharp,aop,aspect,C#,Postsharp,Aop,Aspect,我正在研究PostSharp中的各种概念 更新: 这是我的课程 namespace myconstructor { class Program { static void Main(string[] args) { createfolder(); streamfolder(); } public static void createfolder() {

我正在研究PostSharp中的各种概念

更新:

这是我的课程

namespace myconstructor
{
    class Program
    {
        static void Main(string[] args)
        {
            createfolder();
            streamfolder();
        }
        public static void createfolder()
        {
            File.Create("E:/samplefile.txt");

        }
        public static void streamfolder()
        {
            StreamWriter sw = new StreamWriter("E:/samplestream.txt");
        }
    }

}
和我的同学一样

1)一些跟踪方面类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PostSharp.Extensibility;
using PostSharp.Aspects.Dependencies;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using System.Reflection;
using System.Linq.Expressions;

namespace MyProviders
{
    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Event)]
    [MulticastAttributeUsage(MulticastTargets.Event, AllowMultiple = false)]
    [AspectTypeDependency(AspectDependencyAction.Commute,typeof(SomeTracingAspect))]
    [Serializable]
    public class SomeTracingAspect : EventLevelAspect
    {
        [OnMethodEntryAdvice, MethodPointcut("SelectConstructors")]
        public void OnConstructorEntry(MethodExecutionArgs args)
        {
            args.ReturnValue = "aspectfile"; 
        }

        IEnumerable<ConstructorInfo> SelectConstructors(EventInfo target)
        {
            return target.DeclaringType.GetConstructors(
                        BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
        }

        public override void RuntimeInitialize(EventInfo eventInfo)
        {
            base.RuntimeInitialize(eventInfo);

        }
    }

}
我得到的错误是

 error PS0125: An unexpected exception occured when executing user code: System.ArgumentNullException: Value cannot be null.
 error PS0125: Parameter name: type
 error PS0125:    at System.Activator.CreateInstance(Type type, Boolean nonPublic)
 error PS0125:    at ^7HtKTJrYMoHj.^kfEQVEmN.^jK8C2yxJ()
 error PS0125:    at PostSharp.Sdk.Utilities.ExceptionHelper.ExecuteUserCode[T](MessageLocation messageLocation, Func`1 userCode, Type[] acceptableExceptions)

等待您的解决方案和回复

我认为您的主要问题是您试图在第三方库(mscorlib)上应用方面。你可以看看哪一个对你有帮助。请正式考虑这一点,PostSharp不支持这一点

为了将方面应用于构造函数,您可能可以将a和a与一起使用

当您不能使用
TypeLevelAspect
(例如,您希望将aspect应用于事件)时,我以前使用了和。这允许您手动搜索构造函数

[OnMethodEntryAdvice, MethodPointcut( "SelectConstructors" )]
public void OnConstructorEntry( MethodExecutionArgs args )
{
    ...
}

IEnumerable<ConstructorInfo> SelectConstructors( EventInfo target )
{
    return target.DeclaringType.GetConstructors(
        BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic );
}
[OnMethodEntryAdvice,MethodPointcut(“SelectConstructors”)]
构造入口上的公共无效(MethodExecutionArgs args)
{
...
}
IEnumerable选择器构造函数(EventInfo目标)
{
返回target.DeclaringType.GetConstructors(
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
}
更详细的讨论了我如何应用它来初始化构造函数中的事件


该类的最新完整源代码。自博客发布以来,我做了几处更改,但针对构造函数的原则保持不变。

@DustinDavis您能为我提供解决方案吗?您当前的方法出了什么问题?我可以在[OnMethodEntryAdvice,MethodPointcut(“SelectConstructors”)中替换您的代码选择器构造函数吗用StreamWriter来解决我的问题problem@GowthamanSS我不能100%确定您是否能够调整参数,我建议您尝试一下,但在
OnConstructorEntry()
方法中,您定义了需要在构造函数开始时注入的代码。您可以通过
args.arguments
访问构造函数参数。此外,请务必查看我的更新。可能将方面应用于第三方库不是一个好主意。在过去的两天里,我尝试了你的代码,但没有得到正确的方法或解决方案,好像我的dll有多个构造函数一样。我该如何处理?你能给我提供一些示例代码来解决我的上述问题,这对我继续下去很有用吗
 error PS0125: An unexpected exception occured when executing user code: System.ArgumentNullException: Value cannot be null.
 error PS0125: Parameter name: type
 error PS0125:    at System.Activator.CreateInstance(Type type, Boolean nonPublic)
 error PS0125:    at ^7HtKTJrYMoHj.^kfEQVEmN.^jK8C2yxJ()
 error PS0125:    at PostSharp.Sdk.Utilities.ExceptionHelper.ExecuteUserCode[T](MessageLocation messageLocation, Func`1 userCode, Type[] acceptableExceptions)
[OnMethodEntryAdvice, MethodPointcut( "SelectConstructors" )]
public void OnConstructorEntry( MethodExecutionArgs args )
{
    ...
}

IEnumerable<ConstructorInfo> SelectConstructors( EventInfo target )
{
    return target.DeclaringType.GetConstructors(
        BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic );
}