Giter VIP home page Giter VIP logo

Comments (7)

alex-y-su avatar alex-y-su commented on July 16, 2024

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.

noamtcohen avatar noamtcohen commented on July 16, 2024

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.

ahmedalejo avatar ahmedalejo commented on July 16, 2024

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.

noamtcohen avatar noamtcohen commented on July 16, 2024

Thanks, I found a different way to solve my problem. It was almost 6 months ago but I appreciate you help.

from methoddecorator.

ahmedalejo avatar ahmedalejo commented on July 16, 2024

@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.

noamtcohen avatar noamtcohen commented on July 16, 2024

In the end I didn't use Fody. I don't remember the alternative I used. Sorry....

from methoddecorator.

SimonCropp avatar SimonCropp commented on July 16, 2024

closing this as stale. please raise a new issue if this is still a problem

from methoddecorator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.