Using a custom attribute in aspx pages

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hardieca@hotmail.com

    #1

    Using a custom attribute in aspx pages

    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.GetCustomAt tributes(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]

    #2
    Re: Using a custom attribute in aspx pages

    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.


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

    <hardieca@hotma il.comwrote in message
    news:1184603047 .279859.164770@ n60g2000hse.goo glegroups.com.. .
    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.GetCustomAt tributes(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
    >

    Comment

    • hardieca@hotmail.com

      #3
      Re: Using a custom attribute in aspx pages

      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.P age

      The attribute has been coded as follows:

      [AttributeUsage( (AttributeTarge ts.Class | AttributeTarget s.Method),
      AllowMultiple=t rue)]
      public class ToDoAttribute : System.Attribut e {

      // Private member data
      private string _comment;

      private string _date;

      private string _programmer;

      public ToDoAttribute(s tring programmer, string myDate) {
      this._programme r = 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.GetAs semblies, but attribute decorations on
      partial classes in the ASPX files aren't being recognized.

      protected void Page_Unload(obj ect sender, System.EventArg s e) {
      AppDomain currentDomain = AppDomain.Curre ntDomain;
      Assembly[] assems = currentDomain.G etAssemblies();
      string alltypes = "";
      Assembly assem;
      foreach (assem in assems) {
      Type[] types = assem.GetTypes( );
      foreach (Type t in types) {
      MemberInfo inf = t;
      object[] attributes;
      attributes =
      inf.GetCustomAt tributes(typeof (ToDoAttribute) , false);
      foreach (object attribute in attributes) {
      alltypes = (alltypes + ass.FullName);
      ToDoAttribute todo;
      attribute;
      ToDoAttribute;
      alltypes = (alltypes + ("<p>"
      + (t.ToString() + "<br>")));
      alltypes = (alltypes + ("Programmer : "
      + (todo.Programme r + "<br>")));
      alltypes = (alltypes + ("Date: "
      + (todo.LogDate + "<br>")));
      alltypes = (alltypes + ("Comments: "
      + (todo.Comment + "</p>")));
      }
      }
      }
      this.lblLabel.T ext = alltypes;
      }


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

      Chris

      Comment

      • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

        #4
        RE: Using a custom attribute in aspx pages

        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



        "hardieca@hotma il.com" wrote:
        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.GetCustomAt tributes(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
        >
        >

        Comment

        • hardieca@hotmail.com

          #5
          Re: Using a custom attribute in aspx pages

          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...@yaho o.yabbadabbadoo .comwrote:
          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

          Comment

          Working...