Virtual method with Generic C #

0

I am creating a Base Form where I am not using it of type "Generic" because of problem with forms inheritance in C # with it.

In this basic form, I had to create a virtual method with "Generic", because it will be overwritten in the child class.

Error 1 'Project.REP.Desktop.Format.Error.ObterRegisterParametersGridView (System.Linq.Expressions.Expression>)': no suitable method found to override C: \ Users \ Nicola Bogar \ documents \ visual studio 2013 \ Projects \ Project .ERP.Solution \ Project.ERP.Desktop \ Form.Contest.cs 39 38 Project.REP.Desktop

  

Parent Form

public partial class FormularioBase : Form
{

    public virtual List<object> ObterRegistrosParaPopularGridView<TEntity>(Expression<Func<TEntity, bool>> where = null)
    {
        throw new NotImplementedException("Método ObterRegistrosParaPopularGridView da classe FormularioBase não implentada.");
    }
}
  

Son Form

public partial class FormularioEstado : FormularioBase
{
    public override List<object> ObterRegistrosParaPopularGridView<Estado>(Expression<Func<Estado, bool>> where = null)
    {
        using (ProjetoContext contexto = new ProjetoContext())
        {
            if (where == null)
            {
                return contexto.Estados.Include(x => x.Pais)
                    .Select(x => new
                    {
                        Handle = x.Handle,
                        Descricao = x.Descricao,
                        Sigla = x.Sigla,
                        Pais = x.Pais,
                        PaisHandle = x.PaisHandle,
                        PaisDescricao = x.Pais.Descricao,
                        DataCadastro = x.DataCadastro,
                        DataAlteracao = x.DataAlteracao
                    }).ToList<object>();
            }
            else
            {
                return contexto.Estados
                    .Where(where)
                    .Select(x => new
                    {
                        Handle = x.Handle,
                        Descricao = x.Descricao,
                        Sigla = x.Sigla,
                        Pais = x.Pais,
                        PaisHandle = x.PaisHandle,
                        PaisDescricao = x.Pais.Descricao,
                        DataCadastro = x.DataCadastro,
                        DataAlteracao = x.DataAlteracao
                    }).ToList<object>();
            }

        }
    }
}
    
asked by anonymous 22.09.2017 / 01:21

1 answer

0

Create the generic example example :

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Class1
{
    public virtual List<T>
           ObterRegistrosParaPopularGridView<T>(Expression<Func<T, bool>> where = null)
    {
        throw new NotImplementedException("---");
    }
}

public class Class2: Class1
{
    public override List<User> 
     ObterRegistrosParaPopularGridView<User>(Expression<Func<User, bool>> where = null)
    {
        return base.ObterRegistrosParaPopularGridView(where);
    }
}

In your code it would look something like this:

public partial class FormularioBase : Form
{
    public virtual List<T>
     ObterRegistrosParaPopularGridView<T>(Expression<Func<T, bool>> where = null)
    {
        throw new NotImplementedException("Método ObterRegistrosParaPopularGridView ...");
    }
}

Re-written method:

public override List<Estado> 
 ObterRegistrosParaPopularGridView<Estado>(Expression<Func<Estado, bool>> where = null)
{
    return base.ObterRegistrosParaPopularGridView(where);
}
    
22.09.2017 / 01:37