Attribute Classes - How do I use them?

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

I think I understand what these are for, but am not quite
suer how to use them. I'm assuming that the basic purpose
of the attribute class is to allow me to pull information
from the class at runtime, kind of like the stuff that MFC
did with macros and all the _Runtime_Class things you used
to see in your MFC programs. So, my question is, once I
create an attribute class and apply it, how do I do
something with the stuff I pass into it? For example:

// Here's my attribute class:

[AttributeUsage( AttributeTargets.Class)]
public class HelpStringAttribute : Attribute
{
private string m_value;
public HelpStringAttribute( string Value)
{
m_value = Value;
}

public string Value
{
get { return m_value; }
}
};

// Now let's implement a couple of classes that use it!

[HelpString("This is my MyFirstClass")]
public class MyFirstClass
{
public viod ShowClassName()
{
// Code in here to get that attribute info ???
return;
}
};

[HelpString("This is my MySecondClass")]
public class MySecondClass
{
public viod ShowClassName()
{
// Code in here to get that attribute info ???
return;
}
};

I know this is prob. pretty simple, at least assuming that
I am using this the right way, but I couldn't find
anything in the online docs about "hwo" it's actually
used. Thanks!

JIM
 
[snip]
[HelpString("This is my MySecondClass")]
public class MySecondClass
{
public viod ShowClassName()
{
// Code in here to get that attribute info ???
return;
}
};

I know this is prob. pretty simple, at least assuming that
I am using this the right way, but I couldn't find
anything in the online docs about "hwo" it's actually
used. Thanks!

Jim,

have a look at the Type.GetCustomAttributes(...) method

hth
andrew
 
Back
Top