저는 PostSharp에서 다양한 개념을 연구하고 있습니다.PostSharp를 사용하여 C#에서 생성자에 애스펙트 적용
업데이트 :
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);
}
}
}
2 :이
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) 일부 추적 측면 클래스와 내 측면 클래스는
입니다) TraceAspectProvid 클래스 :
using System; using System.Collections.Generic; using System.Linq; using System.Text; PostSharp.Aspects를 사용하는 ; using System.Reflection;
네임 스페이스 MyProviders { 공용 클래스 TraceAspectProvider : IAspectProvider { 읽기 전용 SomeTracingAspect aspectToApply = 새로운 SomeTracingAspect();
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
Assembly assembly = (Assembly)targetElement;
List<AspectInstance> instances = new List<AspectInstance>();
foreach (Type type in assembly.GetTypes())
{
ProcessType(type, instances);
}
return instances;
}
private void ProcessType(Type type, List<AspectInstance> instances)
{
foreach (ConstructorInfo target in type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
{
instances.Add(new AspectInstance(target, aspectToApply));
}
foreach (Type nestedType in type.GetNestedTypes())
{
ProcessType(nestedType, instances);
}
} } }
그리고 난 솔루션과 응답을 기다리는
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)
으로 오류를 얻고있다
"C:\Program Files\PostSharp 2.1\Release\postsharp.4.0-x86-cil.exe" "D:\fileaspecttest\myconstructor.exe" /p:AspectProviders=MyProviders.AspectProvider,MyProviders /p:Output="D:\fileaspecttest\myaspect.exe"
으로 내 측면 파일은 주어진
@DustinDavis이 문제와 관련하여 해결책을 제공해 줄 수 있습니까? – GowthamanSS
현재 접근 방식에 어떤 문제가 있습니까? –