7

I am using FileHelpers to write out fixed length files.

 public class MyFileLayout
{

    [FieldFixedLength(2)]
    private string prefix;

    [FieldFixedLength(12)]
    private string customerName;

    public string CustomerName
    {
        set 
        { 
            this.customerName= value;
            **Here I require to get the customerName's FieldFixedLength attribute value**

        }
    }
}

As shown above, I would like a access the custom attribute value inside the set method of the property.

How do I achieve this?

vijay
  • 635
  • 2
  • 13
  • 26
  • There's also this, http://stackoverflow.com/questions/6320591/getting-values-from-custom-field-attributes-in-c-sharp – Patrick Aug 13 '13 at 06:48

2 Answers2

8

You can do this using reflection.

using System;
using System.Reflection;

[AttributeUsage(AttributeTargets.Property)]
public class FieldFixedLengthAttribute : Attribute
{
    public int Length { get; set; }
}

public class Person
{
    [FieldFixedLength(Length = 2)]
    public string fileprefix { get; set; }

    [FieldFixedLength(Length = 12)]
    public string customerName { get; set; }
}

public class Test
{
    public static void Main()
    {
        foreach (var prop in typeof(Person).GetProperties())
        {
            var attrs = (FieldFixedLengthAttribute[])prop.GetCustomAttributes
                (typeof(FieldFixedLengthAttribute), false);
            foreach (var attr in attrs)
            {
                Console.WriteLine("{0}: {1}", prop.Name, attr.Length);
            }
        }
    }
}

for more information please refer this

Community
  • 1
  • 1
MSTdev
  • 4,507
  • 2
  • 23
  • 40
4

The only way of doing this is using the reflection:

var fieldInfo = typeof(MyFileLayout).GetField("customerName", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
var length = ((FieldFixedLengthAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(FieldFixedLengthAttribute))).Length;

I've tested it with following FieldFixedLengthAttribute implementation:

public class FieldFixedLengthAttribute : Attribute
{
    public int Length { get; private set; }

    public FieldFixedLengthAttribute(int length)
    {
        Length = length;
    }
}

You have to adapt the code to reflect properties of your attribute class.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • Awesome, Thank you. Almost over, but need a little fix. var length = ((FieldFixedLengthAttribute) Attribute.GetCustomAttribute(fieldInfo, typeof (FieldFixedLengthAttribute))).Length; I am getting this error message: 'FileHelpers.FieldFixedLengthAttribute' does not contain a definition for 'Length' – vijay Aug 13 '13 at 06:57
  • @vijay Sure, I've created my own attribute class, with `Length` property. You have to adapt the code to reflect your attribute class structure. – MarcinJuraszek Aug 13 '13 at 07:03
  • Thank you, FieldFixedLengthAttribute is defined in the FileHelpers library. I am only using the dll. Removing the "Length" at the end is working: var length = ((FieldFixedLengthAttribute) Attribute.GetCustomAttribute(fieldInfo, typeof (FieldFixedLengthAttribute))); But on mouse over I could see FieldFixedLengthAttribute type is already exposing property named "Length", [this is the screenshot link](http://i.imgur.com/2rsyQfs.png). I looked into the FileHelpers lib source code and found that the "Length" property is declared as internal. Is there anyway to extract the value from it? – vijay Aug 13 '13 at 07:21