How do I get the custom attribute value of a field? [duplicate]
Asked Answered
T

2

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?

Tumbler answered 13/8, 2013 at 6:18 Comment(1)
There's also this, #6321091Neologize
H
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

Hypermeter answered 13/8, 2013 at 6:18 Comment(0)
A
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.

Askja answered 13/8, 2013 at 6:28 Comment(3)
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'Tumbler
@Tumbler Sure, I've created my own attribute class, with Length property. You have to adapt the code to reflect your attribute class structure.Askja
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. 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?Tumbler

© 2022 - 2024 — McMap. All rights reserved.