Using model Annotations for C#controller

Asked

Viewed 116 times

1

I have this Annotation on my model

[ZoneamentoDados(fim = 2, inicio = 1, tamanho = 2, obs = "Tipo Registro")]
public String tipoRegistro { get; set; }
[ZoneamentoDados(fim = 54, inicio = 54, tamanho = 1, obs = "Sempre 1")]

need to use end, start and size values to separate substring in the controller. Can someone tell me how I could use these annotations in the controller or indicate some link to better understand?

  • http://www.macoratti.net/13/12/c_vdda.htm

1 answer

1

In this case you will have to work with the famous Reflection.

To do this, you will need to read the selected property and return the desired value.

An example would be something like this:

public static int ObterValorInicio<T>(Expression<Func<T>> expr)
{
    var mexpr = expr.Body as MemberExpression;
    if (mexpr == null) return 0;
    if (mexpr.Member == null) return 0;
    //Busco o CustomAttribute ZoneamentoDados
    object[] attrs = mexpr.Member.GetCustomAttributes(typeof(ZoneamentoDados), false);
    if (attrs == null || attrs.Length == 0) return 0;
    //Seleciono o primeiro valor
    ZoneamentoDados desc = attrs[0] as ZoneamentoDados;
    if (desc == null) return 0;

    //Obtém o valor Inicio. Para obter outros valores, basta alterar, ex: return desc.Tamanho;
    return desc.Inicio;
}

In this example I seek a custom attribution called ZoneamentoDados and return the values. If you find the value, I return the value corresponding to the desired property.

See a full example on .Netfiddle here.

Below are some references for better understanding:

In case the . Netfiddle explodes, below is the complete example:

using System;
using System.Linq.Expressions;

namespace TestReflection
{
    class Program
    {
        static void Main(string[] args)
        {
            var registro = new Registro();

            var inicio = ClassExtensions.ObterValorInicio(() => registro.tipoRegistro);
            Console.WriteLine(inicio);
        }
    }

    public class ZoneamentoDados : System.Attribute
    {
        public int Fim { get; set; }
        public int Inicio { get; set; }
        public int Tamanho { get; set; }
        public string Obs { get; set; }

        public ZoneamentoDados(int fim, int inicio, int tamanho, string obs)
        {
            Fim = fim;
            Inicio = inicio;
            Tamanho = tamanho;
            Obs = obs;
        }
        public ZoneamentoDados() { }
    }


    public class Registro
    {
        [ZoneamentoDados(Fim = 2, Inicio = 1, Tamanho = 2, Obs = "Tipo Registro")]
        public String tipoRegistro { get; set; }
    }

    public static class ClassExtensions
    {
        public static int ObterValorInicio<T>(Expression<Func<T>> expr)
        {
            var mexpr = expr.Body as MemberExpression;
            if (mexpr == null) return 0;
            if (mexpr.Member == null) return 0;
            object[] attrs = mexpr.Member.GetCustomAttributes(typeof(ZoneamentoDados), false);
            if (attrs == null || attrs.Length == 0) return 0;
            ZoneamentoDados desc = attrs[0] as ZoneamentoDados;
            if (desc == null) return 0;

            //Obtém o valor Inicio. Para obter outros valores, basta alterar, ex: return desc.Tamanho;
            return desc.Inicio;
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.