By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,783 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,783 IT Pros & Developers. It's quick & easy.

Custom Attributes not working.

P: n/a
My goal is to provide some additional functionallity inside the
database wrapper if the current object has the LongRunningAttribute
attribute applied. The attribute is never being instantiated by the
framework. What am I doing wrong?
TIA,
Jared
[LongRunning()]
private DatabaseWrapper _database = new DatabaseWrapper();
private void button2_Click(object sender, EventArgs e)
{
_database.ExecuteCommandMethod("some command", 30);
}

[AttributeUsage(AttributeTargets.Field)]
public class LongRunningAttribute : Attribute
{
public LongRunningAttribute(): base()
{
System.Diagnostics.Debug.Write(this.ToString());
}
}
public class DatabaseWrapper
{
private string _test = string.Empty;
public DatabaseWrapper()
{
this._test = "blah";
}
public object ExecuteCommandMethod(string commandText, int
commandTimeout)
{
return null;
}
}

TIA,
Jared

May 12 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Jared,

What do you mean it is never being instantiated? It absolutely is. The
runtime creates an instance of it and applies it to the type the first time
it is used.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jared" <gr***********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
My goal is to provide some additional functionallity inside the
database wrapper if the current object has the LongRunningAttribute
attribute applied. The attribute is never being instantiated by the
framework. What am I doing wrong?
TIA,
Jared
[LongRunning()]
private DatabaseWrapper _database = new DatabaseWrapper();
private void button2_Click(object sender, EventArgs e)
{
_database.ExecuteCommandMethod("some command", 30);
}

[AttributeUsage(AttributeTargets.Field)]
public class LongRunningAttribute : Attribute
{
public LongRunningAttribute(): base()
{
System.Diagnostics.Debug.Write(this.ToString());
}
}
public class DatabaseWrapper
{
private string _test = string.Empty;
public DatabaseWrapper()
{
this._test = "blah";
}
public object ExecuteCommandMethod(string commandText, int
commandTimeout)
{
return null;
}
}

TIA,
Jared

May 12 '06 #2

P: n/a
Nichalos,
First thanks for your reply.

Shouldn't I be able to set a breakpoint in the contstructor of my
attribute class and/or does the diagnostic message display in your
output window?
Neither of these work for me!
Also, when I try to extract the attributes from my current assembly
using the following statement my LongRunning attribute is not contained
within the array.
This I will admit could be due to user error. If I'm trying to extract
the attribute in an incorrect manner please point me in the right
direction.

System.Attribute[] customAttributes =
System.Attribute.GetCustomAttributes(this.GetType( ).Assembly);

Jared

May 12 '06 #3

P: n/a
Sorry...before you critique my code for enumerating the attributes the
database wrapper class has a test attribute set up like the LongRunning
attribute.

[Test()]
public class DatabaseWrapper
{
}

May 12 '06 #4

P: n/a
Jared,

Your call for the custom attributes should be like this:

Attribute[] customAttributes =
this.GetType().GetCustomAttributes(typeof(LongRunn ingAttribute), true);
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jared" <gr***********@gmail.com> wrote in message
news:11*********************@d71g2000cwd.googlegro ups.com...
Nichalos,
First thanks for your reply.

Shouldn't I be able to set a breakpoint in the contstructor of my
attribute class and/or does the diagnostic message display in your
output window?
Neither of these work for me!
Also, when I try to extract the attributes from my current assembly
using the following statement my LongRunning attribute is not contained
within the array.
This I will admit could be due to user error. If I'm trying to extract
the attribute in an incorrect manner please point me in the right
direction.

System.Attribute[] customAttributes =
System.Attribute.GetCustomAttributes(this.GetType( ).Assembly);

Jared

May 12 '06 #5

P: n/a
Sorry (again)! I didn't post the right code.
customAttributes =
(System.Attribute[])System.Reflection.Assembly.GetExecutingAssembly() .GetType().GetCustomAttributes(typeof(LongRunningA ttribute),
false);

May 12 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.