本文介绍了在部分类上创建多态方法以在C#上发送泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对各种部分C#类实现通用的List过程.我将使用这种通用方法来填充组合框以过滤数据.

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:

界面

首先,我创建一个通用接口和一个自定义类

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();
        }
    }

现在我得到一个通用列表,并返回.现在我的问题是我需要通过WCF将其发送给客户端,并且我想要一个通用的(多态的?)方法来做到这一点.到目前为止,我是这样解决的:

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:

方法A

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

方法B

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

所以我的问题是

**是否有一种多态通用方法可以在单个过程中编写方法A和方法B(因为它们以相同的通用类型进行响应)?

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

*这将与WCF服务兼容吗?谢谢你的一切!

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

更新

好的.我完成了一半.现在,它可以正常工作了.我已经像这样修改了我的方法:

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();  
    }

如果我从班级内部调用它,效果很好.但是,如果从WCF Cliente打电话给我,我会得到:

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

有什么想法吗?

更新2

这是我的服务合同:

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

这是我的课程实现:

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>();

推荐答案

您可以尝试以下方法:

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

它强制TIEnumerador并具有无参数构造函数.

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

这样称呼:

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

还有

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

这篇关于在部分类上创建多态方法以在C#上发送泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 15:32