加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 07:43:21 所属栏目:百科 来源:网络整理
导读:我创建一个函数来返回一个字典,我想通过作为参数传递类名.但它给出了一个错误.我写的代码如下 public Dictionaryobject,object GetDetails(Type Classname){ MvcDemoDBEntities db = new MvcDemoDBEntities(); DictionaryClassname,object dict = new Dictio
我创建一个函数来返回一个字典,我想通过作为参数传递类名.但它给出了一个错误.我写的代码如下
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();

人是我的班级名字.

解决方法

使用泛型

你目前的做法会给你带来很多麻烦.当您要为类传递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

当然,这可以使用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();

使用Type

这里的斗争是我们不知道项目类型…所以我将使用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();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读