0

我有这个:

    public class Blah
    {
        public int id { get; set; }
        public string blahh { get; set; }
    }

    public class Doh
    {
        public int id { get; set; }
        public string dohh { get; set; }
        public string mahh { get; set; }
    }

    public List<???prpClass???> Whatever(string prpClass)

其中字符串 prpClass 可以是“Blah”或“Doh”。

根据字符串 prpClass 的内容,我希望 List 类型为 Blah 或 Doh 类。

我怎样才能做到这一点?

编辑:

public List<prpClass??> Whatever(string prpClass)
    {
        using (var ctx = new ApplicationDbContext())
        {
            if (prpClass == "Blah")
            {
                string queryBlah = @"SELECT ... ";

                var result = ctx.Database.SqlQuery<Blah>(queryBlah).ToList();

                return result;
            }
            if (prpClass == "Doh")
            {
                string queryDoh = @"SELECT ... ";

                var result = ctx.Database.SqlQuery<Doh>(queryDoh).ToList();

                return result;
            }

            return null
        }
    }
4

2 回答 2

3

你必须有一个共同的超类型:

 public interface IHaveAnId
 {
      int id { get;set; }
 }

public class Blah : IHaveAnId
{
    public int id { get; set; }
    public string blahh { get; set; }
}

public class Doh : IHaveAnId
{
    public int id {get;set;}
    public string dohh { get; set; }
    public string mahh { get; set; }
}

那么你可以这样做:

public List<IHaveAnId> TheList = new List<IHaveAnId>();

并以某种方式:

TheList.Add(new Blah{id=1,blahh = "someValue"});
TheList.Add(new Doh{id =2, dohh = "someValue", mahh = "someotherValue"});

遍历列表:

foreach(IHaveAnId item in TheList)
{
    Console.WriteLine("TheList contains an item with id {0}", item.id); 
    //item.id is allowed since you access the property of the class over the interface
}

或遍历所有 Blah:

foreach(Blah item in TheList.OfType<Blah>())
{
    Console.WriteLine("TheList contains a Blah with id {0} and blahh ='{1}'", item.id, item.blahh);
}

编辑:

2个方法和一个保存自动值的int字段:

 private int autoValue = 0;     

 public void AddBlah(string blahh)
 {
      TheList.Add(new Blah{id = autovalue++, blahh = blahh});
 }

 public void AddDoh(string dohh, string mahh)
 {
      TheList.Add(new Doh{id = autovalue++, dohh = dohh, mahh = mahh});
 }

另一个编辑

 public List<object> Whatever(string prpClass)
 {
    using (var ctx = new ApplicationDbContext())
    {
        if (prpClass == "Blah")
        {
            string queryBlah = @"SELECT ... ";

            var result = ctx.Database.SqlQuery<Blah>(queryBlah).ToList();

            return result.Cast<object>().ToList();
        }
        if (prpClass == "Doh")
        {
            string queryDoh = @"SELECT ... ";

            var result = ctx.Database.SqlQuery<Doh>(queryDoh).ToList();

            return result.Cast<object>.ToList();
        }

        return null;
    }
}

在视图中,您必须决定它是什么类型。在 asp.net MVC 中,您可以使用显示模板并使用反射来获得良好的设计。但是我仍然不知道您使用的是什么技术。

另一个编辑

测试类:

public class SomeClass
{
    public string Property { get; set; }
}

存储库:

public static class Repository
{
    public static List<object> Whatever(string prpClass)
    {
        switch (prpClass)
        {
            case "SomeClass":
                return new List<SomeClass>() 
                {
                   new SomeClass{Property = "somestring"},
                   new SomeClass{Property = "someOtherString"}
                }.Cast<object>().ToList();
            default:
                return null;

        }
    }
}

以及 mvc 中的控制器操作:

 public JsonResult Test(string className)
 {
    return Json(Repository.Whatever("SomeClass"),JsonRequestBehavior.AllowGet);
 }

然后我调用它:http://localhost:56619/Home/Test?className=SomeClass

并得到了结果:

[{"Property":"somestring"},{"Property":"someOtherString"}]
于 2015-02-25T10:56:57.020 回答
0

这是你想要做的吗?

public class Blah
{
    public int id { get; set; }
    public string blahh { get; set; }
}

public class Doh
{
    public int id { get; set; }
    public string dohh { get; set; }
    public string mahh { get; set; }
}

class Program
{
    public static List<T> Whatever<T>(int count) where T: new()
    {
        return Enumerable.Range(0, count).Select((i) => new T()).ToList();
    }

    static void Main(string[] args)
    {
        var list=Whatever<Doh>(100);
        // list containts 100 of "Doh"
    }
}
于 2015-02-25T13:23:15.723 回答