c# - 根据参数类型调用函数

标签 c# .net list generics interface

我想弄清楚如何简化以下内容

假设我有 2 个实体类

public class A
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
}

public class B
{
    public int Id { get; set; } 
    public string Nom { get; set; }
    public string Ville { get; set; }
} 

相似但不相同的类。

每个类都有一个用于 CRUD 操作的存储库类,例如...

public class RepA
{
    public static List<A> GetAll()
    {
        List<A> list = new List<A>();

        A a1 = new A() {Id=1, Name="First A", City="Boston"};
        A a2 = new A() {Id=2, Name="First B", City="Chicago"};
        A a3 = new A() {Id=3, Name="First C", City="San Francisco"};

        list.Add(a1);
        list.Add(a2);
        list.Add(a3);
        return list;
    }

    public static void SaveAll(List<A> list)
    {
        foreach (A a in list)
        {
              Console.WriteLine("Saved Id = {0} Name = {1} City={2}", 
                  a.Id, a.Name, a.City);
        }
    }

}

public class RepB
    {
        public static List<B> GetAll()
        {
            List<B> list = new List<B>();

            B b1 = new B() {Id=1, Nom="Second A", Ville="Montreal"};
            B b2 = new B() {Id=2, Nom="Second B", Ville="Paris"};
            B b3 = new B() {Id=3, Nom="Second C", Ville="New Orleans"};

            list.Add(b1);
            list.Add(b2);
            list.Add(b3);
            return list;
        }

    public static void SaveAll(List<B> list)
    {
        foreach (B b in list)
        {
            Console.WriteLine("Saved Id = {0} Name = {1} City={2}", b.Id, 
                    b.Nom, b.Ville);
        }
    }

}

我将如何在不诉诸于此的情况下对我的存储库进行匿名调用,因为在我的真实示例中,我有 100 个存储库,而不是 2 个。

void Main()
{
    ChosenType chosentype    = RandomChosenType(); //A or B
    switch (chosentype)
    {
        case ChosenType.A:
            var listA = RepA.GetAll();
            RepA.SaveAll(listA);
            break;
        case ChosenType.B:
            var listB = RepB.GetAll();
            RepB.SaveAll(listB);
            break;
            default:
            break;
    }
}

最佳答案

创建一个基类 或使用interface :

public interface IBase<T>
{
     List<T> GetAll();
     void SaveAll(List<T> items);
}

public class RepA : IBase<RepA> 
{
    public List<RepA> GetAll() { return new List<RepA>(); }
    public void SaveAll(List<RepA> repA) { }
}

public class RepB : IBase<RepB> 
{
    public List<RepB> GetAll() { return new List<RepB>(); }
    public void SaveAll(List<RepB> repB) { }
}

void Main() 
{
    IBase chosenType = RandomChosenType();
    var list = chosenType.GetAll();
}

关于c# - 根据参数类型调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18147182/

相关文章:

c# - TimeZone.IsAmbigouslyTime,为什么它对于 GMT 模糊时间失败?

c# - 带有 PART_Editor 作为 Itemssource 项的 WPF ComboBox

c# - 如何在 CSS 中传递参数,然后如何在 ASP.NET 中对该参数应用 if..else?

.net - 你如何处理 .NET 中生成的 JSON 中的 Infinity

c# - 矩阵和向量的点积

java - 无法理解如何在 java 中定义列表列表

c# - 使用 C# Linq to XML 解析 GML 数据

c# - ListView WP8 中的增量加载

python - 在 python 中创建一个带有列表的数据框

python - 我想异常处理 'list index out of range.'