基于字符串参数的动态类



我有这个:

    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
        }
    }

您必须有一个通用的超类型:

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

编辑:

两个方法和一个保存autovalue的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"}]

这就是您想要做的吗?

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"
    }
}

相关内容

  • 没有找到相关文章

最新更新