Using a custom attribute in aspx pages


H

hardieca

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
 
Ad

Advertisements

N

Nicholas Paldino [.NET/C# MVP]

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.
 
H

hardieca

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 + ass.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
 
G

Guest

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
 
Ad

Advertisements

H

hardieca

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top