Decorating a partial class with a custom attribute

H

hardieca

Hi!

I decorate my unfinished classes and methods with a custom TODO
attribute (as in things To Do). Using reflection, I am then able to
parse through my classes and output all TODOs to a list I can examine
to figure out what is left to be done in my application.

The attribute is coded thusly:

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;
}
}
}

All is well and good until I try to decorate a partial class in the
code-behind of an ASP.Net page like this:

[ToDo("hardie.ca", "2007-07-16", Comment = "Fix recursion!")]
partial class admin_Section : System.Web.UI.Page

The problem is the function that examines all the types in my
application only discovers the TODO decorations if they are decorating
classes or methods that reside in the App_Code folder. I would like to
be able to discover these attributes in webpages that reside outside
of App_Code. I understand that assemblies are generated dynamically
using voodoo I don't fully comprehend, but I would have thought that
by calling AppDomain.currentDomain.GetAssemblies() I would have been
able reference all assemblies.

I am trying to output a webpage that publishes my list of TODOs, is it
possible that the assemblies that contain the pages with the partial
classes I'm decorating have not been created when I try to discover
their attributes? Any help would be very much appreciated!

The page that lists all TODOs has the following method:

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) {
ToDoAttribute todo = (ToDoAttribute)attribute;
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;
}
 
B

bruce barker

there is little voodoo. every page is complied to a dll (if batching is
on, several pages may endup in the same dll). if your site is not
precompiled then the page dll's are created on first reference.

note: asp.net site run from temp folders were all the dll's are copied
to, and new compiles done. this is so site dll's can be updated (avoid
inuse error with copy).

-- bruce (sqlwork.com)

Hi!

I decorate my unfinished classes and methods with a custom TODO
attribute (as in things To Do). Using reflection, I am then able to
parse through my classes and output all TODOs to a list I can examine
to figure out what is left to be done in my application.

The attribute is coded thusly:

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;
}
}
}

All is well and good until I try to decorate a partial class in the
code-behind of an ASP.Net page like this:

[ToDo("hardie.ca", "2007-07-16", Comment = "Fix recursion!")]
partial class admin_Section : System.Web.UI.Page

The problem is the function that examines all the types in my
application only discovers the TODO decorations if they are decorating
classes or methods that reside in the App_Code folder. I would like to
be able to discover these attributes in webpages that reside outside
of App_Code. I understand that assemblies are generated dynamically
using voodoo I don't fully comprehend, but I would have thought that
by calling AppDomain.currentDomain.GetAssemblies() I would have been
able reference all assemblies.

I am trying to output a webpage that publishes my list of TODOs, is it
possible that the assemblies that contain the pages with the partial
classes I'm decorating have not been created when I try to discover
their attributes? Any help would be very much appreciated!

The page that lists all TODOs has the following method:

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) {
ToDoAttribute todo = (ToDoAttribute)attribute;
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;
}
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top