Solved

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

Posted on 2007-04-06
4
7,588 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Desktop Application 3 65
how to remove duplicate code from my project 5 49
IEnumerable<T> to a List<T> 8 54
linq .concat equivalent with expression syntax 4 26
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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