?
Solved

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

Posted on 2007-04-06
4
Medium Priority
?
7,594 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 750 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 750 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

777 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