Comments (7)
Can you give me any example how do you want to use constructor parameters? And how IL code should find and pass values for .ctor parameters?
from methoddecorator.
Hi, I what I meant was being able to pass arguments to the constructor like this:
[Decorator("Name")]
void Login()
{
...
}
So that the value "Name" would be available in the object without reflecting.
Thanks.
from methoddecorator.
I overcomed this limitation by creating a base System.Attribute
class implementing IMethodDecorator. It gets all read/writable properties lazily via reflection. in the constructor
this._properties = new Lazy<IEnumerable<PropertyInfo>>(()=>
this.GetType()
.GetRuntimeProperties()
.Where(p => p.CanRead && p.CanWrite));
then in the Init(object instance, MethodBase method, object[] args)
a update the current instance with the the one anotated on method
public virtual void Init(object instance, MethodBase method, object[] args)
{
this.UpdateFromInstance(method);
//
this.MethodDescription = method.DeclaringType.Name + "." + method.Name;
}
private void UpdateFromInstance(MethodBase method)
{
this.DecoratedMethod = method;
var declaredAttribute = method.GetCustomAttribute(this.GetType());
foreach (var property in this._properties)
property.SetValue(this, property.GetValue(declaredAttribute));
}
Note that this is necessary because MethodDecorator creates a new instance of your method decorating attribute, and doesn´t use the one returned by MethodBase .GetCustomAttribute(Type)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
[module: MethodDecorator]
/// <summary>
/// <see cref="https://github.com/Fody/MethodDecorator"/>
/// <seealso cref="https://github.com/alexeysuvorov/MethodDecorator"/>
/// </summary>
public interface IMethodDecorator
{
void Init(object instance, MethodBase method, object[] args);
void OnEntry();
void OnExit();
void OnException(Exception exception);
}
[AttributeUsage(
AttributeTargets.Module |
AttributeTargets.Method |
AttributeTargets.Assembly |
AttributeTargets.Constructor )]
public class MethodDecoratorAttribute : Attribute, IMethodDecorator
{
private Lazy<IEnumerable<PropertyInfo>> _properties;
public MethodBase DecoratedMethod { get; private set; }
public MethodDecoratorAttribute()
{
this._properties = new Lazy<IEnumerable<PropertyInfo>>(()=>
this.GetType()
.GetRuntimeProperties()
.Where(p => p.CanRead && p.CanWrite));
}
public string MethodDescription { get; private set; }
public virtual void Init(object instance, MethodBase method, object[] args)
{
this.UpdateFromInstance(method);
//
this.MethodDescription = method.DeclaringType.Name + "." + method.Name;
}
public virtual void OnEntry() { }
public virtual void OnExit() { }
public virtual void OnException(Exception exception) { }
private void UpdateFromInstance(MethodBase method)
{
this.DecoratedMethod = method;
var declaredAttribute = method.GetCustomAttribute(this.GetType());
foreach (var property in this._properties.Value)
property.SetValue(this, property.GetValue(declaredAttribute));
}
}
from methoddecorator.
Thanks, I found a different way to solve my problem. It was almost 6 months ago but I appreciate you help.
from methoddecorator.
@noamtcohen could you share your "way"
i did mine months ago as well, but since i temporarily stopped using MethodDecorator, i decided to share what i learnt.
from methoddecorator.
In the end I didn't use Fody. I don't remember the alternative I used. Sorry....
from methoddecorator.
closing this as stale. please raise a new issue if this is still a problem
from methoddecorator.
Related Issues (20)
- MethodDecorator attribute on method in interface should be cascaded to implementation. HOT 1
- Icon, package url are broken HOT 2
- Update to Fody 6
- Interceptor Attribute in Class Library not Triggered HOT 10
- Decide whether to rethrow exception HOT 2
- Ability to decorate all methods in project HOT 1
- Debugging decorated methods yields "Source Not Available" error. HOT 4
- OnExit is invoked before the end of the function happens. HOT 9
- How about `Attribute.GetCustomAttributes()` performance? HOT 3
- Ability to control throwing exception HOT 3
- Add an interceptor type that wraps the method execution HOT 2
- System.IO.FileLoadException on dotnet 5 HOT 1
- .NET Core support HOT 2
- Compatibility with new Fody HOT 8
- Can Fody be used for handling exceptions? HOT 4
- Build Error after adding MethodDecorator.Fody to the project HOT 1
- Constructor support of instance reference HOT 2
- Can't view static variables in debug mode HOT 1
- Async equivalents of OnEntry and OnExit HOT 2
- Flag to prevent rethrow #26 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from methoddecorator.