Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add attributes to a method at runtime?

We're using Microsoft.Practices.CompositeUI.EventBroker to handle event subscription and publication in our application. The way that works is that you add an attribute to your event, specifying a topic name, like this:

[EventPublication("example", PublicationScope.Global)]
public event EventHandler Example;

then you add another attribute to your handler, with the same topic name, like this:

[EventSubscription("example", ThreadOption.Publisher)]
public void OnExample(object sender, EventArgs e)
{
    ...
}

Then you pass your objects to an EventInspector which matches everything up.

We need to debug this, so we're trying to create a debug class that subscribes to all the events. I can get a list of all the topic names... but only at runtime. So I need to be able to add attributes to a method at runtime, before we pass our debug object to the EventInspector.

How do I add attributes to a method at runtime?

like image 905
Simon Avatar asked Nov 06 '08 12:11

Simon


People also ask

How do I add attributes to my property at runtime?

It is not possible to add Attributes in run-time. Attributes are static and cannot be added or removed.

How to add attribute to existing class Python?

Adding attributes to a Python class is very straight forward, you just use the '. ' operator after an instance of the class with whatever arbitrary name you want the attribute to be called, followed by its value.

How to add attribute to variable Python?

How to Use the setattr() to Add a New Attribute to a Class in Python. If you want to add a new field to the class, you may use the setattr() method with the same syntax. The setattr() adds a new variable in an existing class and sets the stated data when the given variable doesn't occur in the class.

How do I add attributes to a list?

Right-click in the list to which you want to add a new attribute name and click Add new attribute. A text box appears in the list. Type the name of the attribute in the text box. Press the TAB key.


2 Answers

What you are trying to achieve is quite complicated, so I will try to provide something just to get you started. This is what I think you would need to combine in order to achieve something:

  1. Define an abstract class AbstractEventDebugger, with a method Search that searches all of the event members, and registers them with the EventInspector. Also, define a method IdentifyEvent that will allow you to identify the event that has called it (this depends on you - what parameters will have, etc.).
  2. Define a dynamic type using TypeBuilder (as described here), that inherits from your class. This class would be the class of your debugger object.
  3. Attach the Handlers to your class using Reflection.Emit.MethodBuilder (see here), which will be calling the IdentifyEvent method from the parent class and,
  4. Reflection.Emit the attributes on the handlers using CustomAttributeBuilder class (see here).
  5. Create an instance of your dynamic class and send it to the EventInspector.
  6. Fire it up :)

Here is a sample on how to create a method that calls something (Actually it's the classic "Hello world").

You will need to do a lot of tweaking in order to get it done well, but you will learn a lot about reflection.

Good luck!

like image 66
Bogdan Maxim Avatar answered Oct 19 '22 09:10

Bogdan Maxim


Attributes are a compile-time feature (unless you are dealing with ComponentModel - but I suspect it is using reflection). As such, you cannot add attributes at runtime. It would a similar question to "how do I add an extra method to a type at runtime?". In regular C# / .NET (pre-DLR), you can't.

like image 26
Marc Gravell Avatar answered Oct 19 '22 09:10

Marc Gravell