R
Rowan
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?
}
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?
}