Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将类类型作为参数c传递并与字典一起使用_C#_Class_Dictionary_Parameters - Fatal编程技术网

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

C# 将类类型作为参数c传递并与字典一起使用,c#,class,dictionary,parameters,C#,Class,Dictionary,Parameters,我正在创建一个函数来返回一个字典,我想把类名作为一个参数传递给大家。但它给出了一个错误。下面给出了我编写的代码 public Dictionary<object, object> GetDetails(Type Classname) { MvcDemoDBEntities db = new MvcDemoDBEntities(); Dictionary<Classname, object> dict = new Dictionary<Classnam

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

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();
人是我的类名。

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

List<people> list = GetDetails(typeof(people)).Keys.ToList();
使用泛型 你目前的做法会给你带来很多麻烦。当您要为类传递一个类型对象时,您将需要反射才能创建字典

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

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;
}
您可能有兴趣将人员更改为与传入的类名称匹配的属性。这只能通过反射进行存档。与我们获取字典的Add方法的方式类似,我们可以从对象db获取一个属性,该属性的名称由参数类型给出

我将把它作为第一个调用的第二个方法

使用泛型 为此:

var data = (from h in GetCollection<TClass>(db) select h).ToList();
var data = (from h in GetCollection(db, Class).Cast<object>() select h).ToList();
要使用,请替换此:

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

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

它是类类型,而不是类名。我编辑了你的问题,以便更详细accurate@NayeemMansoori你能说得更具体些吗?另外,我将编辑我的答案以展示反射方法,尽管这需要一些时间。我使用了您的函数如下------------------公共字典GetDetails{Dictionary dict=new Dictionary;dict{dict.AddTClass,true;}中的foreach var项return dict;}但它给出了一个如何在中添加类值的错误dictionary@NayeemMansoori您必须使用item作为键,而不是TClass,TClass是类型。请参阅更新的答案。编辑:修正了打字错误。我的问题不是给你答案answers@NayeemMansoori我是那个试图给你答案的人,因为我对这个问题的理解越全面越好。我想,你还没能解决你的问题。既然你还没有告诉我怎么做,或者为什么,我最好的猜测是它与这行var data=from h in db有关。人们选择h.ToList;。我会尽量扩大我的回答范围,希望能涵盖你问题的解决方案。
var data = (from h in GetCollection<TClass>(db) select h).ToList();
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();