Я не думаю, что вам нужна библиотека, чтобы прочитать свойства FieldAttribute.
public class MyFileLayout
{
[FieldFixedLength(2)]
public string prefix;
[FieldFixedLength(12)]
public string customerName;
}
Type type = typeof(MyFileLayout);
FieldInfo fieldInfo = type.GetField("prefix");
object[] attributes = fieldInfo.GetCustomAttributes(false);
FieldFixedLengthAttribute attribute = (FieldFixedLengthAttribute)attributes.FirstOrDefault(item => item is FieldFixedLengthAttribute);
if (attribute != null)
{
// read info
}
Я сделал метод для этого:
public bool TryGetFieldLength(Type type, string fieldName, out int length)
{
length = 0;
FieldInfo fieldInfo = type.GetField(fieldName);
if (fieldInfo == null)
return false;
object[] attributes = fieldInfo.GetCustomAttributes(false);
FieldFixedLengthAttribute attribute = (FieldFixedLengthAttribute)attributes.FirstOrDefault(item => item is FieldFixedLengthAttribute);
if (attribute == null)
return false;
length = attribute.Length;
return true;
}
Использование:
int length;
if (TryGetFieldLength(typeof(MyFileLayout), "prefix", out length))
{
Show(length);
}
PS: поля/свойства должны быть открытыми для того, чтобы читать их атрибуты с отражением.
Вы хотите получить значения атрибутов? – Matten
Да, но это специфично для библиотеки FileHelpers, я уже разместил вопрос о получении значения атрибута, [http://stackoverflow.com/questions/18201971/how-do-i-get-the-custom-attribute-value -of-a-field /] (http://stackoverflow.com/questions/18201971/how-do-i-get-the-custom-attribute-value-of-a-field/) – vijay