Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.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中使用GetValues和SetValues#_C# - Fatal编程技术网

C# 无法在项目C中使用GetValues和SetValues#

C# 无法在项目C中使用GetValues和SetValues#,c#,C#,我想将数据行转换为对象。我写了一个类来做这个。 错误如下: 方法“SetValue”不重载2个参数 方法“GetValue”不重载1个参数 但是我不能使用GetValues()和SetValues()。将项目转换为4.5时。这是工作。 我的项目设定的平台目标是3.5(强制性-因为我连接设备必须使用.NET3.5) 如何解决这个问题 这是我的代码: public DataRowToObject(DataRow row) { List<PropertyInfo&

我想将数据行转换为对象。我写了一个类来做这个。 错误如下:

方法“SetValue”不重载2个参数

方法“GetValue”不重载1个参数

但是我不能使用GetValues()和SetValues()。将项目转换为4.5时。这是工作。 我的项目设定的平台目标是3.5(强制性-因为我连接设备必须使用.NET3.5)

如何解决这个问题

这是我的代码:

    public DataRowToObject(DataRow row)
    {
        List<PropertyInfo> listProperty = this.GetProperties();
        foreach (PropertyInfo prop in listProperty)
        {
            if (!row.Table.Columns.Contains(prop.Name) ||
                row[prop.Name] == null ||
                row[prop.Name] == DBNull.Value)
            {
                prop.SetValue(this, null);
                continue;
            }

            try
            {
                object value = Convert.ChangeType(row[prop.Name], prop.PropertyType);
                prop.SetValue(this, value);
            }
            catch
            {
                prop.SetValue(this, null);
            }
        }
    }
    public virtual Hashtable GetParameters()
    {
        Type type = this.GetType();
        List<PropertyInfo> listProperty = new List<PropertyInfo>(type.GetProperties());

        Hashtable result = new Hashtable();
        foreach (PropertyInfo prop in listProperty)
        {
            result.Add(prop.Name, prop.GetValue(this));
        }

        return result;
    }
公共数据行对象(数据行)
{
List listProperty=this.GetProperties();
foreach(listProperty中的PropertyInfo属性)
{
如果(!row.Table.Columns.Contains)(prop.Name)||
行[prop.Name]==null||
行[prop.Name]==DBNull.Value)
{
prop.SetValue(该值为空);
继续;
}
尝试
{
对象值=Convert.ChangeType(行[prop.Name],prop.PropertyType);
道具设定值(此,值);
}
抓住
{
prop.SetValue(该值为空);
}
}
}
公共虚拟哈希表GetParameters()
{
Type Type=this.GetType();
List listProperty=新列表(type.GetProperties());
Hashtable result=新的Hashtable();
foreach(listProperty中的PropertyInfo属性)
{
添加(prop.Name,prop.GetValue(this));
}
返回结果;
}

在.NET 4.5中没有添加索引器的情况下,
PropertyInfo.SetValue
PropertyInfo.GetValue
有一个重载

但这只是将
null
传递给以前版本的索引器参数(使用和重载)的问题

因此:


这应该适用于.NET.3.5(最新版本)。。。实际上,对于NET2.0及以上版本:-)

@Bharadwaj:这有什么关系?@JonSkeet OP正在使用4.5,但想要部署到3.5,是不是这样的链接不会有帮助?@Bharadwaj不,一点也没有。你真的读过这个问题吗?@Jcl-hmm,也许我理解错了,你能解释一下OP到底想要什么吗?我按自己的理解发表了评论
prop.SetValue(this, value, null);
prop.GetValue(this, null);