Solved

Add attributes to methods at runtime?  (C#, .NET 2.0 Reflection)

Posted on 2007-04-06
4
7,579 Views
Last Modified: 2010-04-10
What I am hoping for is a way to accept a method pointer as a parameter in a subroutine (no problem there) and then apply attributes to that method.  I am building a CAB-based (Microsoft Composite UI Application Block) application and want to encapsulate some of the functionality that the module developer will need.  This is a C# project in .NET 2.0.  For those familiar with CAB, I want to encapsulate the addition of a toolbar item, including creating a Command decorating the clickHandler with the CommandHandler attribute.  
I am able to get the Attributes property from the method using reflection, but this doesnt have any option for Add.  I don't know if what I am trying to do is even possible.  I need some help.  Thanks.
0
Comment
Question by:dougschultz
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
Yttribium earned 250 total points
ID: 18875801
Since your question is still unanswered, I'm going to take a shot in the dark and assume you're looking for a PropertyBag, where you might at sometime during the code want to use a Property that's not defined in a class like

MyObject.Property["NotADefinedPropertyInOrignalClass"].Value = SomeValue;
And would even serialize as such, and if you use a "property" that DOES exist using this method, it would overwrite the base value.
like
MyObject.Name = "Tester";
MyObject.Properties["Name"].Value = "Propertied Tester";

The value of MyObject.Name is now "Propertied Tester"

If this is what you're looking for, invest some time in reading this article, it would explain how to achieve this goal:

http://www.codeproject.com/csharp/propertybag.asp

Hope this helps, I found this article pretty useful for any future need for such features.
0
 

Author Comment

by:dougschultz
ID: 18876835
I will be more clear about what I need.  In use of Microsoft's Composite UI Application Block, commands are often used to communicate Toolbar button clicks to the handler within the module.  To accomplish this, it is necessary for the module to create the command and to tell CAB that a particular method should handle the invokation of the command.  This would done something like the following:

[CommandHandler("CommandName")]
public void toolbarButton_Click(object sender, EventArgs e)
{
            //TODO: Handle button click
}

What I hope to do is encapsulate the creation of the command and the tagging of this method.  This way, the module developer would not need to know about the commands at all.  What I would do then is provide some method which will take information about the toolbar button, as well as a reference to the above handler method.   I could then apply the CommandHandler attribute to that method.

Any clues?
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 250 total points
ID: 18886195
This may seem like a bit of overkill but the ICustomTypeDescriptor interface may work for you in many cases http://msdn2.microsoft.com/en-us/library/system.componentmodel.icustomtypedescriptor.aspx but not in this particular one ...

Now this will sound like REAL overkill but what you will have to do here is generate at runtime a deriving class which adds the attribute :-/

I know this isnt what you are looking for as its not a simple answer but its the way to do it :(

Cheers,

Greg

0
 

Author Comment

by:dougschultz
ID: 19196491
Thanks guys, but it doesnt appear that this is possible, at least not without doing some crazy stuff like building a class at runtime, which will not meet my needs anyway.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now