471,325 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Custom Attribute trouble

Hello,

I've been dinking around with using Custom Attributes at the
encouragement of an answer to a previous post and found it pretty easy
to create and set them. But now I am having a lot of trouble figuring
out how to get the values out. I think I am overcomplicating it. (I am
going to leave the terribly awful code I am messing with out.)

I have the following:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class fooFieldAttribute : System.Attribute
{
public int RequiredLength { get; set; }
public bool LeadingZeros { get; set; }
public fooFieldAttribute()
{
}
}
public class fooHeader
{
private string _HeaderIdentifier = "H1"
[fooField(RequiredLength = 2, LeadingZeros = false)]
public string HeaderIdentifier
{
get { return _HeaderIdentifier ; }
}

private string _Count = "1"
[fooField(RequiredLength = 3, LeadingZeros = true)]
public string Count
{
get { return _Count ; }
}
private string FormatWithPadding(string str, int reqlength, bool
leadingzeros)
{
bool useDefault = string.IsNullOrEmpty(str);

Int32 i = reqlength - str.Trim().Length;
if (leadingzeros && !useDefault)
{
return str.Trim().PadLeft(i, '0');
}
else
{
return str.Trim().PadRight(i, ' ');
}
}

Now I want to do something like:

public void Write(fooHeader fooheader)
{
StringBuilder strHeader = new StringBuilder();
strHeader.Append = FormatWithPadding(fooHeader.HeaderIdentifier,
fooFieldAttribute.RequiredLength for HeaderIdentifier,
fooFieldAttribute.LeadingZeros for HeaderIdentifier);
strHeader.Append = FormatWithPadding(fooHeader.Count,
fooFieldAttribute.RequiredLength for Count,
fooFieldAttribute.LeadingZeros for Count);

Can anyone help?

}

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
3 954
On Fri, 23 May 2008 16:35:23 -0700, Rowan <an***********@yahoo.comwrote:
Hello,

I've been dinking around with using Custom Attributes at the
encouragement of an answer to a previous post and found it pretty easy
to create and set them. But now I am having a lot of trouble figuring
out how to get the values out. I think I am overcomplicating it. (I am
going to leave the terribly awful code I am messing with out.)

[...]
Can anyone help?
AFAIK, you need to use reflection (put this method somewhere convenient):

static fooFieldAttribute GetFooFieldAttribute(Type type, string
strMember)
{
object[] rgobj =
type.GetMember(strMember).GetCustomAttributes(type of(fooFieldAttribute),
true);

// If the attribute is present, return the first instance
if (rgobj.Length 0)
{
return rgobj[0];
}

return null;
}

Then you could write:

public void Write(fooHeader fooheader)
{
StringBuilder strHeader = new StringBuilder();
fooFieldAttribute ffaHeader = GetFooFieldAttribute(typeof(fooHeader),
"HeaderIdentifier"),
ffaCount = GetFooFieldAttribute(typeof(fooHeader), "Count");

strHeader.Append = FormatWithPadding(fooHeader.HeaderIdentifier,
ffaHeader.RequiredLength, ffaHeader.LeadingZeros);
strHeader.Append = FormatWithPadding(fooHeader.Count,
ffaCount.RequiredLength, ffaCount.LeadingZeros);
}

Or something like that. I didn't bother to compile any of that, so my
apologies in advance for typos or minor conceptual problems. :)

Pete
Jun 27 '08 #2
Thanks...this has been a very big help. I am having a little trouble
with a cast and am unsure what to do.

When I try to return rgobj[0]; I get the error
Cannot implicitly convert type 'object' to ...fooFieldAttribute'. An
explicit conversion exists (are you missing a cast?)

Is there something easy I am overlooking? I've tried quite a few
variations with no luck.
Jun 27 '08 #3
Actually, I think I got it...return rgobj[0] as fooFieldAttribute;
works! I appreciate your help...soooo much better than what I was
messing with.

On May 27, 12:06*pm, Rowan <MaryERo...@gmail.comwrote:
Thanks...this has been a very big help. *I am having a little trouble
with a cast and am unsure what to do.

When I try to return rgobj[0]; I get the error
Cannot implicitly convert type 'object' to ...fooFieldAttribute'. An
explicit conversion exists (are you missing a cast?)

Is there something easy I am overlooking? *I've tried quite a few
variations with no luck.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by F. Da Costa | last post: by
8 posts views Thread by Martin Lapierre | last post: by
3 posts views Thread by Edward Diener | last post: by
21 posts views Thread by One Handed Man \( OHM - Terry Burns \) | last post: by
reply views Thread by Will Waggoner | last post: by
7 posts views Thread by Julian Jelfs | last post: by
3 posts views Thread by Mr Newbie | last post: by
5 posts views Thread by =?Utf-8?B?cGFnYXRlcw==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.