Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Using a custom attribute in aspx pages

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hardieca@hotmail.com - 16 Jul 2007 17:24 GMT
Hi!

I have built a custom attribute that tracks my list of things left to
do in my application:

[ToDo("hardie.ca", "2007-07-16", Comment:="Fix recursion")]
Public Class SomeClass ...

Using reflection, I am able to find all attributes within the App_Code
folder:

Assembly a = Assembly.Load("__code")
string alltypes = "";
Type[] types = a.GetTypes();
foreach (Type t in types) {
    MemberInfo inf = t;

    object[] attributes;

    attributes = inf.GetCustomAttributes(typeof(ToDoAttribute), false);

    foreach (object attribute in attributes) {
        ToDoAttribute todo = (ToDoAttribute)attribute;
        alltypes += "<p>" + t.ToString() + "<br>";
        alltypes += "Programmer: " + todo.Programmer + "<br>";
        alltypes += "Date: " + todo.LogDate + "<br>";
        alltypes += "Comments: " + todo.Comment + "</p>";
    }
}

My problem is I would like to decorate the classes of Aspx pages, but
because they don't reside within the App_code folder, I can't get a
reference to their assembly. Is there some way to do this?

Thanks,

Chris
Nicholas Paldino [.NET/C# MVP] - 16 Jul 2007 17:33 GMT
If you want to decorate the classes of the ASPX pages, then you should
be able to just place it on the class in your partial class file.
Attributes placed on the class there should be on the class as well when the
ASP.NET engine compiles it.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi!
>
[quoted text clipped - 33 lines]
>
> Chris
hardieca@hotmail.com - 16 Jul 2007 18:39 GMT
Unfortunately, that doesn't seem to be the case. I've decorated a page
with my attribute like this:

[ToDo("hardie.ca", "2007-07-16", Comment = "Do not hardcode the site
ID!")]
partial class admin_Section : System.Web.UI.Page

The attribute has been coded as follows:

[AttributeUsage((AttributeTargets.Class | AttributeTargets.Method),
AllowMultiple=true)]
public class ToDoAttribute : System.Attribute {

   //  Private member data
   private string _comment;

   private string _date;

   private string _programmer;

   public ToDoAttribute(string programmer, string myDate) {
       this._programmer = programmer;
       this._date = myDate;
   }

   public string Comment {
       get {
           return _comment;
       }
       set {
           _comment = value;
       }
   }

   public string LogDate {
       get {
           return _date;
       }
   }

   public string Programmer {
       get {
           return _programmer;
       }
   }
}

But the page that lists all the attributes only finds them when
they're in app_code. I can see all the assemblies generated by viewing
the output of AppDomain.GetAssemblies, but attribute decorations on
partial classes in the ASPX files aren't being recognized.

protected void Page_Unload(object sender, System.EventArgs e) {
       AppDomain currentDomain = AppDomain.CurrentDomain;
       Assembly[] assems = currentDomain.GetAssemblies();
       string alltypes = "";
       Assembly assem;
       foreach (assem in assems) {
           Type[] types = assem.GetTypes();
           foreach (Type t in types) {
               MemberInfo inf = t;
               object[] attributes;
               attributes =
inf.GetCustomAttributes(typeof(ToDoAttribute), false);
               foreach (object attribute in attributes) {
                   alltypes = (alltypes + a.s.FullName);
                   ToDoAttribute todo;
                   attribute;
                   ToDoAttribute;
                   alltypes = (alltypes + ("<p>"
                               + (t.ToString() + "<br>")));
                   alltypes = (alltypes + ("Programmer: "
                               + (todo.Programmer + "<br>")));
                   alltypes = (alltypes + ("Date: "
                               + (todo.LogDate + "<br>")));
                   alltypes = (alltypes + ("Comments: "
                               + (todo.Comment + "</p>")));
               }
           }
       }
       this.lblLabel.Text = alltypes;
   }

Anyone have any ideas why I can't decorate the partial class in aspx
pages?

Chris
Peter Bromberg [C# MVP] - 16 Jul 2007 18:40 GMT
I think the problem you may be having is that you are using the Web Site
project model, which creates all kinds of separate little assemblies with
funky names that are hard to track. If you switch to the Web Application
Project model, you get a single assembly for all your pages' codebehind and
it will be much easier to track the little boogers.
-- Peter
Site:  http://www.eggheadcafe.com
UnBlog:  http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA):    http://www.blogmetafinder.com

> Hi!
>
[quoted text clipped - 33 lines]
>
> Chris
hardieca@hotmail.com - 16 Jul 2007 18:51 GMT
To advertise my ignorance, how would I go about do that? If I have a
pre-existing project, can I switch over or do i have to create a new
web app project? I guess I don't understand the difference between a
web app project and a website project. Switching over from Python,
still getting my bearings :)

On Jul 16, 1:40 pm, Peter Bromberg [C# MVP]
<pbromb...@yahoo.yabbadabbadoo.com> wrote:
> I think the problem you may be having is that you are using the Web Site
> project model, which creates all kinds of separate little assemblies with
> funky names that are hard to track. If you switch to the Web Application
> Project model, you get a single assembly for all your pages' codebehind and
> it will be much easier to track the little boogers.
> -- Peter

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.