Retrieving attribute information from field

W

Walter L. Williams

I have defined my own IntegerRangeAttribute which is used like so:

[IntegerRange(DefaultValue = 2000, MinimumValue = 0, MaximumValue =
1000000)]
private Int32 iNumBurnInIterations = 2000;

At another point I want to get at that information, ie:

public Int32 GetNumPreIterations ()
{
IntegerRangeAttribute range =
GetIntegerRange(this.iNumBurnInIterations);

/* Do something here */

return (this.iNumBurnInIterations);
}

private IntegerRangeAttribute GetIntegerRange (object o)
{
MemberInfo[] ami = this.GetType().GetMembers(BindingFlags.Instance |
BindingFlags.NonPublic);
foreach (MemberInfo mi in ami)
{
if (mi.Name == o.ToString())
return ((IntegerRangeAttribute)
System.Attribute.GetCustomAttribute(mi, typeof(IntegerRangeAttribute)));
}

return (null);
}


Now the problem is, o.ToString() calls the Int32.ToString(), and the name
won't match up. I don't know of a good way to get that string
programmatically.

Is there a way to get that variable name? Is there a better way to retrieve
the attribute?


====================================================
Walter Williams
Software Engineer
Sawtooth Software, Inc.
http://www.sawtoothsoftware.com
 
M

Mattias Sjögren

Is there a way to get that variable name?

Not from the int value, no. You have to use the variable name as a
string, "iNumBurnInIterations".



Mattias
 

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