/// <summary> /// Construtora /// </summary> public Reg() { tp = typeof(T); RegLayout rl = (RegLayout)Attribute.GetCustomAttribute(tp, typeof(RegLayout)); if (rl != null) { DateFormat6 = rl.DateFormat6 ?? DateFormat6; DateFormat8 = rl.DateFormat8 ?? DateFormat8; DateFormat10 = rl.DateFormat10 ?? DateFormat10; DateFormat12 = rl.DateFormat12 ?? DateFormat12; DateFormat14 = rl.DateFormat14 ?? DateFormat14; Acentos = rl.Acentos; Upper = rl.Upper; } fields = rft.GetFields(tp); valuesRF = new SortedList <string, RegFormat>(); valuesItem = new SortedList <string, object>(); RegFormat rf; foreach (string cField in fields) { rf = RegFormat.Get(tp.GetField(cField)); valuesRF.Add(cField, rf); valuesItem.Add(cField, rf.GetDefault()); } }
protected void Page_Load(object sender, EventArgs e) { // Atributos é outro recurso avançado do .Net. // É comum ve-los nas difinições de seguranças de metodos e parametros de web services // Exemplo 2 ) // Aqui é mostrado a listagem da estrutura de registros de um enumerador com atributos do tipo 'RegFormat' // será usado lblOut.Text = ""; // é possivel obtem as definições sem haver uma instancia, chamando o typeof de um definição qualquer (class, struc, enum) Type tp = typeof(CNAB400Remessa1Bradesco); int n = 1; // aqui será listado os campos do enumerador via 'GetFields()' generico, mas a classe Enum tem metodos mais eficases foreach (FieldInfo fi in tp.GetFields()) { // em um enumerador, o campo valor, é uma variável especial, oculto na programação, mas visivel pela reflection if (fi.IsSpecialName) { continue; } // A classe 'Attribute', obtem um atributo de um campo // E por poder haver mais de um atributo, deve-se especificar qual o tipo de atributo estamos querendo obter RegFormat rf = (RegFormat)Attribute.GetCustomAttribute(fi, typeof(RegFormat)); // se o atributo não for encontrado, retorna NULL if (rf == null) { continue; } // Formata o resultado lblOut.Text += string.Format("<i>{0:000}</i> <b>{1}({2:00})</b> {3}<br/>\r\n", n, rf.Type, rf.Length, fi.Name); n += rf.Length; // calcula a posição de forma incremental com a soma de todos os comprimentos } lblOut.Text += "<b>TOTAL: " + (n - 1) + " caracteres</b>"; // apenas remove 1 para ajustar ao inicio que é base 1 }
/// <summary> /// Limpa o valor da variável /// </summary> //public void Clear() //{ // lineValue = null; // if (Value == null) // { // if (Default != null && _type == RegType.PX) // Value = Default; // else if (Default != null && _type == RegType.P9) // Value = int.Parse(Default); // else if (_type == RegType.P9 || _type == RegType.PV) // Value = 0; // else if (_type == RegType.PX) // Value = ""; // } // } internal static RegFormat Get(FieldInfo fi) { RegFormat rf; Type tp = fi.DeclaringType; rf = (RegFormat)Attribute.GetCustomAttribute(fi, typeof(RegFormat)); if (rf == null) { string[] erf = fi.Name.Split('_'); if (erf.Length != 3) { throw new Exception("Enumerador não contem auto definição no formato 'Nome_Tipo_Tamanho', e nem atributo de 'RegFormat'"); } RegType t = (RegType)Enum.Parse(typeof(RegType), erf[1]); int l = Int32.Parse((erf[2])); rf = new RegFormat(t, l); } //rf.Clear(); return(rf); }