Solved

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

Posted on 2007-04-06
4
7,585 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

821 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