将类类型作为参数传递到C#中,并与字典一起使用

7
我正在创建一个函数来返回一个字典,我想通过参数传递类名。但是它会出现错误。我写的代码如下:
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();

people是我的类名。


这是类类型,而不是类名。我修改了你的问题,使其更准确。 - Kamil Budziewski
2个回答

8

使用泛型

你目前的方法会带来很多麻烦。因为你将要传递一个类的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();

使用类型

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

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,你能具体说明一下吗?另外,我会编辑我的答案来呈现反射方法,但这需要一些时间。 - Theraot
我像这样使用了您的函数------------------------------ public Dictionary<object, object> GetDetails<TClass>() { Dictionary<TClass, object> dict = new Dictionary<TClass, object>(); foreach (var item in dict) { dict.Add(TClass, true); } return dict; } 但是它会出现错误,如何在字典中添加类值 - Nayeem Mansoori
@NayeemMansoori 你必须使用item作为键,而不是TClass,TClass是类型。请查看更新后的答案。编辑:修正了拼写错误。 - Theraot
#我的问题是没有解决,无法给你答案。 - Nayeem Mansoori
@NayeemMansoori 我是在尽力给你答案的人, 为此我对问题的理解越完整就越好。我觉得你还没有能够解决你的问题。既然你并没有告诉我如何或为什么,我最好的猜测是这行代码“var data = (from h in db.People select h).ToList();”有关。我会尽量扩展我的回答,以期覆盖解决你问题的方案。 - Theraot

2
您需要使用 typeof 来传递您的类的类型。
List<people> list = GetDetails(typeof(people)).Keys.ToList();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接