Create Polymorphic method on partial class to send generic type on c#

为君一笑 提交于 2019-12-11 10:57:41

问题


I want to implement a generic List procedure to various partial C# classes. I will use this generic method to fill comboboxes for filtering data.

So far i have try this:

Interface

First I create a generic interface and a custom class

public class Enumerador
{
    public int ID{ get; set; }
    public string Description{ get; set; }
}

interface IEnumerador
{
    IEnumerable<Enumerador> Enumerar();
}

Then on every class i want this behavior I implement this interface like this:

 public IEnumerable<Enumerador> Enumerar()
    {
        using (var context = new OhmioEntities())
        {
            Clientes _allClients = new Clientes();
            _allClients.RazonFantasia = "Any Client";
            _allClients.ID_Cliente = -1;
            var query = context.Clientes;
            var _clients = query.Where(f => f.Activo == true).OrderBy(o => RazonFantasia).ToList();
            _clients.Insert(0, _allClients);

            var p = from cli in _clients
                    select new Enumerador { ID = cli.ID_Cliente, Description = cli.RazonFantasia };
            return p.ToList();
        }
    }

Now i get a generic list filled and returned. Now my problem is that i need to send this to client over WCF, and i want a generic (polymorphic?) method to do that. So far i resolve it like this:

Method A

public IEnumerable<Enumerador> GetClients()
    {
        Clientes _cli = new Clientes();
        return _cli.Enumerar();            
    }

Method B

public IEnumerable<Enumerador> GetVendors()
    {
        Vendors _vnd = new Vendors();
        return _vnd.Enumerar();            
    }

So my questions are

*Is there a polymorphic generic way to write Method A and Method B on a single procedure (Because they respond with same generic type)?

*Will this be compatible with WCF service? Thanks for everything!

UPDATE

Ok. I half way done. Now it almos work. I've modified my method like this:

public IEnumerable<Enumerador> GetEnumerador<T>() where T : IEnumerador, new()
    {
        T _obj = new T();
        return _obj.Enumerar();  
    }

If i call it from within the class work great. But if a call it from WCF Cliente i get:

The non generic Method 'Ohmio.Client.OhmioService.OhmioServiceClient.GetEnumerador()' cannot be used with types arguments

Any Idea?

UPDATE 2

This is my service contract:

public interface IOhmioService
{        
    [OperationContract]
    IEnumerable<Enumerador> GetEnumerador<T>() where T : IEnumerador, new();        
}

This is my class implementation:

public class OhmioService : IOhmioService
{
    public IEnumerable<Enumerador> GetEnumerador<T>() where T : IEnumerador, new()
    {
        T _obj = new T();
        return _obj.Enumerar();  
    }
}

And call it from client like this:

public IEnumerable<Enumerador> Clients { get; set; } 
Clients = this.serviceClient.GetEnumerador<Clientes>();

回答1:


You could try something like this:

public IEnumerable<Enumerador> GetClients<T>() where T : IEnumerador, new()
{
    T _cli = new T();
    return _cli.Enumerar();            
}

It forces T to be IEnumerador and have a parameterless constructor.

Call it like this:

IEnumerable<Enumerador> c = GetClients<Clientes>();

And

IEnumerable<Enumerador> v = GetClients<Vendors>();


来源:https://stackoverflow.com/questions/23366706/create-polymorphic-method-on-partial-class-to-send-generic-type-on-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!