arxone / mradvice Goto Github PK
View Code? Open in Web Editor NEW.NET aspect weaver (build task under NuGet package)
License: MIT License
.NET aspect weaver (build task under NuGet package)
License: MIT License
Allow to rename target (with a Regex?).
When advised method takes no parameter, a new object[0]
is created anyway. A more compact version could use a null
instead.
Apply advices depending on configuration (Release, Debug).
The best way would be an attribute applied to advices (such as ConfigurationDependent
)
The Handle<> can be used from a generic method written by the user.
In this case, we need to search where this method is used and what parameter is provided.
Weaving generates invalid code (or not code at all) for some methods.
See project BlueDwarf (class ConfigurationViewModel) for example.
Auto dependency properties are not found by the XAML designer.
Import auto dependency properties from Blue Dwarf project and create a designer page.
It works with PostSharp, so it should work with Weavisor 😕
Example code:
[MyAdvice]
class Foo
{
public object this[string bar]
{
get { return new object(); }
}
public object this[int bar]
{
get { return new object(); }
}
}
Gives error:
Error 1 Fody/MrAdvice.Weaver: Error while weaving method 'System.Object MrAdviceBug.Foo::get_Item(System.Int32)': System.InvalidOperationException: Sequence contains more than one matching element
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
at ArxOne.MrAdvice.Reflection.Groups.MethodReflectionNode.LoadParent()
at ArxOne.MrAdvice.Reflection.Groups.ReflectionNode.<GetSelfAndAncestors>d__14.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.<ReverseIterator>d__a0`1.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
at System.Linq.Enumerable.<DistinctIterator>d__81`1.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at ArxOne.MrAdvice.Weaver.AspectWeaver.WeaveIntroductions(MethodDefinition method, TypeDefinition adviceInterface, ModuleDefinition moduleDefinition, Types types)
at ArxOne.MrAdvice.Weaver.AspectWeaver.WeaveMethod(ModuleDefinition moduleDefinition, MarkedNode markedMethod, TypeDefinition adviceInterface, Types types) MrAdviceBug
Embed XML docs in package (currently, it's a bit dry)
Even if invoked several times, the WeavingContext.AddInitializer
method has to produce only one injection per distinct parameter.
I received following errors when i tried use MrAdvice with abstract method and property
[MyCustomAdvice]
abstract class Foo
{
public abstract void InternalRefresh(bool bar = true);
public abstract bool KeepAlive { get; }
}
Error 11 Fody/MrAdvice.Weaver: Error while weaving method 'System.Boolean Mpos.Modules.Common.ViewModels.Transactions.TransactionListViewModel::get_KeepAlive()': System.NullReferenceException: Object reference not set to an instance of an object.
at ArxOne.MrAdvice.Weaver.AspectWeaver.WeaveAdvices(MethodDefinition method)
at ArxOne.MrAdvice.Weaver.AspectWeaver.WeaveMethod(ModuleDefinition moduleDefinition, MethodDefinition method, TypeDefinition adviceInterface) Mpos.Modules.Common
Error 10 Fody/MrAdvice.Weaver: Error while weaving method 'System.Void Mpos.Modules.Common.ViewModels.Transactions.TransactionListViewModel::InternalRefresh(System.Boolean)': System.NullReferenceException: Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu.
w ArxOne.MrAdvice.Weaver.AspectWeaver.WeaveAdvices(MethodDefinition method)
w ArxOne.MrAdvice.Weaver.AspectWeaver.WeaveMethod(ModuleDefinition moduleDefinition, MethodDefinition method, TypeDefinition adviceInterface) Mpos.Modules.Common
Best Regards
Based on same mechanism as target renaming (Regex)
Currently, the assembly is not located in its right place, making the continuous integration builds fail.
Allow to create interfaces implementation using one or many aspects at type level.
Ideas:
// may take any arbitrary number of aspects
var i = Weavisor.Factory.Create<Interface,MyAdvice>();
// or
// takes only one aspect but is much nicer
var j = MyAdvice.Handle<Interface>();
Currently, MrAdvice.dll has to be referenced from classes using the aspect itself.
If the aspect does not reference Mr Advice classes or interfaces, then there is no need to reference Mr Advice from client assembly.
Allow to advise constructors.
Example code:
[MyAdvice]
class Foo
{
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int memcmp(byte[] b1, byte[] b2, UIntPtr count);
}
Gives error:
Error 1 Fody/MrAdvice.Weaver: Error while weaving method 'System.Int32 Example.Foo::memcmp(System.Byte[],System.Byte[],System.UIntPtr)': System.NullReferenceException: Object reference not set to an instance of an object.
at ArxOne.MrAdvice.Weaver.AspectWeaver.WeaveAdvices(MarkedNode markedMethod)
at ArxOne.MrAdvice.Weaver.AspectWeaver.WeaveMethod(ModuleDefinition moduleDefinition, MarkedNode markedMethod, TypeDefinition adviceInterface, Types types) MrAdviceBug
Hi,
I'm wondering about aspect separation for projects. For example I want to have:
ProjName.Aspects
ProjName.Client
Client want to use aspects from library but unfortunatelly they are not injected into classes in Client project. If aspects are defined in Client project everything works fine.
Needed when doing exception handling and other aspects with post processing code.
When an advice is injected at assembly-level, if it is present in this assembly, then it is advised.
A good solution would be to generate an error at weave-time.
Apparently a parameter advice applied to method or type fails to inject.
If an assembly-level advice uses static field introduction, it will confuse fields between all types.
Using Pepita?
And possibly remove my parameter.Index optimization.
Currently, properties can be handled as methods.
We may need to setup a new interface to handle properties properly.
If a class B inherits from advice A, not sure B is weaved.
Error 1 Fody: PEVerify of the assembly failed.
[IL]: Error: [C:\Code\Fody\FodyAddinSamples\MrAdviceSample\bin\Debug\MrAdviceSample.dll : MrAdviceSample::MyMethodWithAdvice][offset 0x00000008]
initlocals must be set for verifiable methods with one or more local variables.
1 Error(s) Verifying C:\Code\Fody\FodyAddinSamples\MrAdviceSample\bin\Debug\MrAdviceSample.dll
MrAdviceSample
Make it inherited.
Setup wiki pages here, and add readme file to target project linking to it.
Hi,
After upgrading MrAdvice from MrAdvice.Fody.1.1.9 to MrAdvice.Fody.1.2.3 i'm getting following error during build:
Severity Code Description Project File Line Suppression State
Error Fody: An unhandled exception occurred:
Exception:
Exception has been thrown by the target of an invocation.
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at ModuleWeaver.Execute()
at lambda_method(Closure , Object )
at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 161
at InnerWeaver.Execute() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 82
Source:
mscorlib
TargetSite:
System.Object InvokeMethod(System.Object, System.Object[], System.Signature, Boolean)
Could not load file or assembly 'MrAdvice, Version=1.2.3.0, Culture=neutral, PublicKeyToken=c0e7e6eab6f293d8' or one of its dependencies. An error relating to remoting occurred. (Exception from HRESULT: 0x8013150B)
StackTrace:
at ArxOne.MrAdvice.Weaver.AspectWeaver.Weave(ModuleDefinition moduleDefinition)
at ModuleWeaver.Execute()
Source:
MrAdvice.Weaver
TargetSite:
Void Weave(Mono.Cecil.ModuleDefinition)
Object '/d7c98439_1587_4fcf_8984_6e3dbb9f50b3/wqxikqsmin706d4l+zcg+rd3_403.rem' has been disconnected or does not exist at the server.
StackTrace:
Server stack trace:
at System.Runtime.Remoting.Channels.ChannelServices.CheckDisconnectedOrCreateWellKnownObject(IMessage msg)
at System.Runtime.Remoting.Channels.ChannelServices.SyncDispatchMessage(IMessage msg)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at ILogger.LogInfo(String message)
at InnerWeaver.b__88_1(String s) in C:\projects\fody\FodyIsolated\WeaverInitialiser.cs:line 25
at ModuleWeaver.OnAssemblyResolve(Object sender, ResolveEventArgs args)
at System.AppDomain.OnAssemblyResolveEvent(RuntimeAssembly assembly, String assemblyFullName)
Source:
mscorlib
TargetSite:
System.Runtime.Remoting.ServerIdentity CheckDisconnectedOrCreateWellKnownObject(System.Runtime.Remoting.Messaging.IMessage)
Best regards
Currently, exceptions are wrapped in TargetInvocationException 😦
Project can't be compiled after referencing latest (1.1.9.0) Nuget MrAdvice.Foody (just added reference, nothing else was added)(Win 10 64bit .NET 4.5):
Severity Code Description Project File Line
Error Fody: An unhandled exception occurred:
Exception:
Could not load file or assembly 'Mono.Cecil, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
StackTrace:
at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
at System.Reflection.RuntimeMethodInfo.FetchNonReturnParameters()
at System.Reflection.RuntimeMethodInfo.GetParametersNoCopy()
at System.Reflection.RuntimePropertyInfo.GetIndexParametersNoCopy()
at System.Reflection.RuntimePropertyInfo.GetIndexParameters()
at System.RuntimeType.GetPropertyCandidates(String name, BindingFlags bindingAttr, Type[] types, Boolean allowPrefixLookup)
at System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
at System.Type.GetProperty(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
at PropertyDelegateBuilder.BuildPropertySetDelegate[T](Type type, String propertyName) in c:\ConsoleBuildAgent\work\ed448661dbb30d2e\FodyIsolated\DelegateBuilders\PropertyDelegateBuilder.cs:line 9
at DelegateBuilder.BuildDelegateHolder(Type weaverType) in c:\ConsoleBuildAgent\work\ed448661dbb30d2e\FodyIsolated\DelegateBuilders\DelegateBuilder.cs:line 25
at DelegateBuilder.GetDelegateHolderFromCache(Type weaverType) in c:\ConsoleBuildAgent\work\ed448661dbb30d2e\FodyIsolated\DelegateBuilders\DelegateBuilder.cs:line 16
at InnerWeaver.InitialiseWeavers() in c:\ConsoleBuildAgent\work\ed448661dbb30d2e\FodyIsolated\InnerWeaver.cs:line 111
at InnerWeaver.Execute() in c:\ConsoleBuildAgent\work\ed448661dbb30d2e\FodyIsolated\InnerWeaver.cs:line 61
Source:
mscorlib
TargetSite:
Void GetSignature(Void*, Int32, System.RuntimeFieldHandleInternal, System.IRuntimeMethodInfo, System.RuntimeType) GM_YadroModule4
Hi,
I receive following error from fody during build (VS2013):
Error 1 Fody: An unhandled exception occurred:
Exception:
One or more errors occurred.
StackTrace:
at System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose)
at System.Linq.Parallel.SpoolingTask.SpoolForAll[TInputOutput,TIgnoreKey](QueryTaskGroupState groupState, PartitionedStream2 partitions, TaskScheduler taskScheduler) at System.Linq.Parallel.DefaultMergeHelper
2.System.Linq.Parallel.IMergeHelper.Execute()
at System.Linq.Parallel.MergeExecutor1.Execute() at System.Linq.Parallel.MergeExecutor
1.Execute[TKey](PartitionedStream2 partitions, Boolean ignoreOutput, ParallelMergeOptions options, TaskScheduler taskScheduler, Boolean isOrdered, CancellationState cancellationState, Int32 queryId) at System.Linq.Parallel.PartitionedStreamMerger
1.Receive[TKey](PartitionedStream2 partitionedStream) at System.Linq.Parallel.ForAllOperator
1.WrapPartitionedStream[TKey](PartitionedStream2 inputStream, IPartitionedStreamRecipient
1 recipient, Boolean preferStriping, QuerySettings settings)
at System.Linq.Parallel.UnaryQueryOperator2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream
2 inputStream)
at System.Linq.Parallel.ListQueryResults1.GivePartitionedStream(IPartitionedStreamRecipient
1 recipient)
at System.Linq.Parallel.UnaryQueryOperator2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient
1 recipient)
at System.Linq.Parallel.QueryOperator1.GetOpenedEnumerator(Nullable
1 mergeOptions, Boolean suppressOrder, Boolean forEffect, QuerySettings querySettings)
at System.Linq.Parallel.ForAllOperator1.RunSynchronously() at System.Linq.ParallelEnumerable.ForAll[TSource](ParallelQuery
1 source, Action1 action) at ArxOne.MrAdvice.Weaver.AspectWeaver.Weave(ModuleDefinition moduleDefinition) at ModuleWeaver.Execute() at lambda_method(Closure , Object ) at InnerWeaver.ExecuteWeavers() in c:\ConsoleBuildAgent\work\ed448661dbb30d2e\FodyIsolated\InnerWeaver.cs:line 144 at InnerWeaver.Execute() in c:\ConsoleBuildAgent\work\ed448661dbb30d2e\FodyIsolated\InnerWeaver.cs:line 62 Source: System.Core TargetSite: Void QueryEnd(Boolean) opcode StackTrace: at Mono.Cecil.Cil.Instruction.Create(OpCode opcode, Int32 value) at ArxOne.MrAdvice.Weaver.Instructions.EmitLdarg(Int32 index) at ArxOne.MrAdvice.Weaver.AspectWeaver.WritePointcutBody(MethodDefinition method, MethodDefinition innerMethod) at ArxOne.MrAdvice.Weaver.AspectWeaver.WeaveAdvices(MethodDefinition method) at ArxOne.MrAdvice.Weaver.AspectWeaver.<>c__DisplayClass5.<Weave>b__0(MethodDefinition m) at System.Linq.Parallel.ForAllOperator
1.ForAllEnumerator1.MoveNext(TInput& currentElement, Int32& currentKey) at System.Linq.Parallel.ForAllSpoolingTask
2.SpoolingWork()
at System.Linq.Parallel.SpoolingTaskBase.Work()
at System.Linq.Parallel.QueryTask.BaseWork(Object unused)
at System.Linq.Parallel.QueryTask.RunTaskSynchronously(Object o)
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
Source:
Mono.Cecil
TargetSite:
Mono.Cecil.Cil.Instruction Create(Mono.Cecil.Cil.OpCode, Int32)
Loggerv2
Here is a sample code to recreate the issue.
public class Test
{
[MyProudAdvice] // this will pass
public void Method1(FooClass fooClass)
{
}
[MyProudAdvice] // this will pass
public void Method1A(FooClass foo1, FooClass foo2)
{
}
[MyProudAdvice] // fody error
public void Method2(FooClass fooClass, string name, long id, long count)
{
}
[MyProudAdvice] // fody error
public void Method3(string name, FooClass fooClass, long id, long count)
{
}
[MyProudAdvice] // fody error
public void Method4(string name, long id, FooClass fooClass, long count)
{
}
[MyProudAdvice] // fody error
public void Method5(string name, long id, long count, FooClass fooClass)
{
}
[MyProudAdvice] // fody error
public void Method6(FooStruct fooStruct, string name, long id, long count)
{
}
[MyProudAdvice] // fody error
public void Method6(FooEnum fooEnum, string name, long id, long count)
{
}
}
public enum FooEnum
{
}
public struct FooStruct
{
public int Id { get; set; }
}
public class FooClass
{
public int Id { get; set; }
}
public class MyProudAdvice : Attribute, IMethodAdvice
{
public void Advise(MethodAdviceContext context)
{
// do things you want here
context.Proceed(); // this calls the original method
// do other things here
}
}
.NET supports accessing private fields and methods by reflection, but other frameworks such as Silverlight don't.
Adjust visibility depending on target.
The following program fails PEVerify:
using System;
using ArxOne.MrAdvice.Advice;
[assembly: LoggingDemo.LogAdvice]
namespace LoggingDemo
{
class Program
{
static void Main(string[] args)
{
}
}
[AttributeUsage(AttributeTargets.Assembly)]
public class LogAdvice : Attribute, IMethodAdvice
{
public void Advise(MethodAdviceContext context)
{
context.Proceed();
}
}
}
The error:
Error 1 Fody: PEVerify of the assembly failed.
Microsoft (R) .NET Framework PE Verifier. Version 4.0.30319.33440
Copyright (c) Microsoft Corporation. All rights reserved.
[IL]: Error: [c:\users\u\documents\visual studio 2013\Projects\LoggingDemo\LoggingDemo\bin\Debug\LoggingDemo.exe : LoggingDemo.Program::.ctor][offset 0x00000001][found <uninitialized> ref ('this' ptr) 'LoggingDemo.Program'] Uninitialized item on stack.(Error: 0x80131859)
[IL]: Error: [c:\users\u\documents\visual studio 2013\Projects\LoggingDemo\LoggingDemo\bin\Debug\LoggingDemo.exe : LoggingDemo.Program::.ctor?][offset 0x00000001][found ref ('this' ptr) 'LoggingDemo.Program'] Call to .ctor only allowed to initialize this pointer from within a .ctor. Try newobj.(Error: 0x801318BF)
[IL]: Error: [c:\users\u\documents\visual studio 2013\Projects\LoggingDemo\LoggingDemo\bin\Debug\LoggingDemo.exe : LoggingDemo.LogAdvice::.ctor][offset 0x00000001][found <uninitialized> ref ('this' ptr) 'LoggingDemo.LogAdvice'] Uninitialized item on stack.(Error: 0x80131859)
[IL]: Error: [c:\users\u\documents\visual studio 2013\Projects\LoggingDemo\LoggingDemo\bin\Debug\LoggingDemo.exe : LoggingDemo.LogAdvice::.ctor?][offset 0x00000001][found ref ('this' ptr) 'LoggingDemo.LogAdvice'] Call to .ctor only allowed to initialize this pointer from within a .ctor. Try newobj.(Error: 0x801318BF)
4 Error(s) Verifying c:\users\u\documents\visual studio 2013\Projects\LoggingDemo\LoggingDemo\bin\Debug\LoggingDemo.exe
LoggingDemo
Currently, only fields are introduced.
Find a specific marker allowing to remove the backing field from an auto-property.
It should only introduce one member, not the property and its backing field.
In order to inject specific data for aspect (storing data in aspect is unsafe).
Make methods weaving parallel (see if we can gain some performance here)
Hi,
I received following error when i tried compile my project.
It happens because constructor of some class has more than 8 parameters.
Error 11 Fody/MrAdvice.Weaver: Error while weaving method 'System.Void Mpos.Modules.Selling.ViewModels.ShoppingCart.ShoppingCartViewModel::.ctor(Mpos.Shared.Services.Contracts.Navigation.IBreadCrumb,Mpos.Shared.Services.Contracts.Dialogs.IBlockerService,Mpos.Shared.Services.Contracts.Logging.ILogger,Mpos.Shared.Model.Proxy.Events.IEventsService,Mpos.Shared.Model.Proxy.Reservation.IReservationService,Mpos.Shared.Services.Contracts.Dialogs.INotificationService,Mpos.Shared.Services.Contracts.Printer.IPrinterService,Mpos.Shared.Services.Contracts.Configuration.IConfigurationService,Mpos.Shared.Services.Contracts.User.ISessionManager,Mpos.Shared.Services.Contracts.Dialogs.IMessageDialogService)': System.ArgumentException: opcode
w Mono.Cecil.Cil.Instruction.Create(OpCode opcode, Byte value)
w ArxOne.MrAdvice.Weaver.Instructions.EmitLdc(Int32 value)
w ArxOne.MrAdvice.Weaver.AspectWeaver.WritePointcutBody(MethodDefinition method, MethodDefinition innerMethod)
w ArxOne.MrAdvice.Weaver.AspectWeaver.WeaveAdvices(MethodDefinition method)
w ArxOne.MrAdvice.Weaver.AspectWeaver.WeaveMethod(ModuleDefinition moduleDefinition, MethodDefinition method, TypeDefinition adviceInterface) Mpos.Modules.Selling
Best regards
Currently, they are excluded from weaving.
Curently, weavisor does probably not work when placed before an obfuscator, because it uses hard coded strings to identify target method.
Currently the method is the one from the implementation class, which no one know (and no one cares about).
Weaving the small tests take up to 400ms, which is big.
Setup some audit to why it is so long.
Create tests to see if they are handled correctly.
Allow target assembly to add custom weaving.
Yes, this is huge.
Allow to intercept method parameters
The weaver currently does not support generics.
Running PeVerfiy on the output of this https://github.com/Fody/FodyAddinSamples/tree/master/WeavisorSample results in
Error 5 Fody: PEVerify of the assembly failed.
Microsoft (R) .NET Framework PE Verifier. Version 4.0.30319.33440
Copyright (c) Microsoft Corporation. All rights reserved.
[IL]: Error: [C:\Code\Fody\FodyAddinSamples\WeavisorSample\bin\Debug\WeavisorSample.dll : WeavisorSample::MyMethodWithAdvice][offset 0x00000008] initlocals must be set for verifiable methods with one or more local variables.
1 Error(s) Verifying C:\Code\Fody\FodyAddinSamples\WeavisorSample\bin\Debug\WeavisorSample.dll
WeavisorSample
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.