基于字符串参数的动态类

时间:2015-02-25 10:45:43

标签: c# dynamic dynamic-class-creation dynamic-class

我有这个:

    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”。

我希望List类型是基于字符串prpClass所持有的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
        }
    }

2 个答案:

答案 0 :(得分: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
}

或迭代所有Blahs:

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

修改

2个方法和一个包含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中,您可以使用显示模板并使用反射来获得良好的设计。但是,我仍然不知道你正在使用什么技术。

又一个编辑

识别TestClass:

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

答案 1 :(得分: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"
    }
}