Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

618 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