【问题标题】:pass class type as parameter c# and used with dictionary将类类型作为参数 c# 传递并与字典一起使用
【发布时间】:2013-12-07 04:20:05
【问题描述】:

我正在创建一个函数来返回一个字典,并且我想将类名作为参数传递。但它给出了一个错误。我写的代码如下

public Dictionary<object, object> GetDetails(Type Classname)
{
    MvcDemoDBEntities db = new MvcDemoDBEntities();
    Dictionary<Classname, object> dict = new Dictionary<Classname, object>();

    var data = (from h in db.People
                select h).ToList();

    foreach (var item in data)
    {
        dict.Add(data, true);
    }
    return dict;
}

我做错了什么 我想用类名动态调用这个函数,像这样:

List<people> list = GetDetails(people).Keys.ToList();

人们是我的班级名称。

【问题讨论】:

  • 这是类类型,而不是类名。我编辑了你的问题,更准确

标签: c# class dictionary parameters


【解决方案1】:

使用泛型

您当前的方法会给您带来很多麻烦。当你要为你的类传递一个 Type 对象时,你需要反射才能创建Dictionary

作为替代方案,我建议您创建一个泛型方法:

public Dictionary<object, object> GetDetails<TClass>()
{
    MvcDemoDBEntities db = new MvcDemoDBEntities();
    Dictionary<TClass, object> dict = new Dictionary<TClass, object>();

    var data = (from h in db.People
            select h).ToList();

    foreach (var item in data)
    {
        dict.Add(data, true);
    }
    return dict;
}

像这样使用它:

List<people> list = GetDetails<people>().Keys.ToList();

使用类型

当然,这可以使用 Type 对象来完成,这需要使用反射来创建我们不知道类型的对象(该对象是字典)。这样做如下:

public Dictionary<object, object> GetDetails(Type Class)
{
    //Null check
    if (null == Class)
    {
        throw new ArgumentNullException("Class");
    }

    MvcDemoDBEntities db = new MvcDemoDBEntities();

    //Get the generic dictionary type:
    Type DictType = typeof(Dictionary<,>).MakeGenericType(Class, typeof(object));

    //Create the dictionary object:
    object dict = Activator.CreateInstance(typeof(DictType));

    //Get the Add method:
    var add = DictType.GetMethod("Add", new Type[]{Class, typeof(object)});

    var data = (from h in db.People
            select h).ToList();

    foreach (var item in data)
    {
        //add to the dictionary:
        add.Invoke(dict, new object[]{item, true});
    }

    return dict;
}

这样使用:

List<people> list = GetDetails(typeof(people)).Keys.ToList();

深入挖掘

我注意到你有这行:

var data = (from h in db.People select h).ToList();

您可能有兴趣将People 更改为与您传入的类的名称匹配的属性。这只能通过反射存档。与我们获取字典的Add 方法类似,我们可以从对象db 中获取一个属性,该属性的名称由参数类型给出。

我将把它作为第二种方法来介绍,供第一种方法调用。


使用泛型

public IEnumerable<TClass> GetCollection<TClass>(MvcDemoDBEntities db)
{
    //get the type of db
    var dbType = db.GetType();
    //get the name of the type TClass
    var name = typeof(TClass).Name;
    //get the property
    var prop = dbType.GetProperty(name);
    //read the property and return
    return prop.GetValue(db);
}

要使用,请替换:

var data = (from h in db.People select h).ToList();

有了这个:

var data = (from h in GetCollection<TClass>(db) select h).ToList();

使用类型

问题在于我们不知道项目类型...所以我将使用 IEnumerable。

public IEnumerable GetCollection(MvcDemoDBEntities db, Type Class)
{
    //get the type of db
    var dbType = db.GetType();
    //get the name of the type Class
    var name = Class.Name;
    //get the property
    var prop = dbType.GetProperty(name);
    //read the property and return
    return prop.GetValue(db);
}

要使用,请替换:

var data = (from h in db.People select h).ToList();

有了这个:

var data = (from h in GetCollection(db, Class).Cast<object>() select h).ToList();

【讨论】:

  • @NayeemMansoori 你能更具体一点吗?另外,我将编辑我的答案以介绍反思方法,尽管这需要我一段时间。
  • 我这样使用你的函数 ------------------------------ public Dictionary GetDetails() { Dictionary dict = new Dictionary(); foreach (var item in dict) { dict.Add(TClass, true); } 返回字典;但它给出了一个错误如何在字典中添加类值
  • @NayeemMansoori 您必须使用 item 作为键,而不是 TClass,TClass 是类型。请参阅更新的答案。编辑:修正错字。
  • #Theroat my problem is not solve to give you answers
  • @NayeemMansoori 我是试图给你答案的人,因为我对问题的理解越完整越好。我认为,您一直无法解决您的问题。由于您没有告诉我如何或为什么,我最好的猜测是它与“var data = (from h in db.People select h).ToList();”这一行有关。我将尝试扩展我的答案以涵盖尽可能多的内容,希望涵盖您的问题的解决方案。
【解决方案2】:

你需要使用typeof来传递你的类的类型

List<people> list = GetDetails(typeof(people)).Keys.ToList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2015-04-12
    • 1970-01-01
    • 2017-08-14
    相关资源
    最近更新 更多