Note that there are some explanatory texts on larger screens.

plurals
  1. POApplying aspect on constructor in c# using PostSharp
    primarykey
    data
    text
    <p>I am working on various concepts in PostSharp.</p> <p>Updated:</p> <p>This is my program class as</p> <pre><code>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"); } } } </code></pre> <p>and my aspect class as</p> <p><strong>1)some tracing aspect class :</strong></p> <pre><code>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&lt;ConstructorInfo&gt; SelectConstructors(EventInfo target) { return target.DeclaringType.GetConstructors( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); } public override void RuntimeInitialize(EventInfo eventInfo) { base.RuntimeInitialize(eventInfo); } } } </code></pre> <p><strong>2)TraceAspectProvider class:</strong></p> <p>using System; using System.Collections.Generic; using System.Linq; using System.Text; using PostSharp.Aspects; using System.Reflection;</p> <p>namespace MyProviders { public class TraceAspectProvider : IAspectProvider { readonly SomeTracingAspect aspectToApply = new SomeTracingAspect();</p> <pre><code> public IEnumerable&lt;AspectInstance&gt; ProvideAspects(object targetElement) { Assembly assembly = (Assembly)targetElement; List&lt;AspectInstance&gt; instances = new List&lt;AspectInstance&gt;(); foreach (Type type in assembly.GetTypes()) { ProcessType(type, instances); } return instances; } private void ProcessType(Type type, List&lt;AspectInstance&gt; 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); } </code></pre> <p>} } } </p> <p>and my aspect file given as</p> <pre><code> "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" </code></pre> <p>i am getting error as</p> <pre><code> 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) </code></pre> <p>Waiting for your solution and responses</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload