我需要执行一个通用的演员c# [英] I need to perform a generic cast c#

查看:83
本文介绍了我需要执行一个通用的演员c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个继承自动态对象的类,在这个类中,我创建了一个函数,可以转换为代码所在的任何类型的对象:

i'm working with a class that inherits from a dynamic object , in this class I've created a function that can cast to any type of object in which the code goes like this:

foreach (var tmpObject in _Props)
{
    String TmpString=GetKey(tmpObject.Key,BdInfo);
   _Props[tmpObject.Key] = Convert.ChangeType(rst.Fields[TmpString].Value, tmpObject.Value.GetType()); 
}



第一个变量只是存储数据库数据的对象。执行此函数时,我在Convert.changeType上出错。是否有另一种方法可以根据对象的类型创建一个通用的强制转换



我尝试过的方法:



_Props [tmpObject.Key] = Convert.ChangeType(rst.Fields [TmpString] .Value,tmpObject.Value.GetType());


the rst variable is just an object where the database data is stored. When executing this function, I get an error on the Convert.changeType. Is there another way to create a generic cast based on the type of object

What I have tried:

_Props[tmpObject.Key] = Convert.ChangeType(rst.Fields[TmpString].Value,tmpObject.Value.GetType());

推荐答案

我解决了它,问题是在itterator var tmpObject中它无法适应其他类型

所以如果我的第一个元素是一个整数var varine一个整数所以我用了一个访问我的字典中存在的信息的不同方式

i solved it , the problem was in the itterator var tmpObject it can't adapt to other types
so if my first element was an integer the var remine an integer so i used a different way to access informations that exists in my dictionary
for (int i = 0; i < _Props.Count; i++)
           {
               String TmpString = GetKey(_Props.Keys.ElementAt(i), BdInfo);
               _Props[_Props.Keys.ElementAt(i)] = Convert.ChangeType(rst.Fields[TmpString].Value, _Props.Keys.ElementAt(i).GetType());

           }


这篇关于我需要执行一个通用的演员c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆