我想将DataRow转换为Object。我写一堂课。
像这样的错误:

No overload for method 'SetValue' takes 2 arguments

No overload for method 'GetValue' takes 1 arguments

但是我不能使用GetValues()和SetValues()。将项目转换为4.5时。是工作。
我的项目将平台目标设置为3.5(强制性-因为我必须使用.NET 3.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;
    }

最佳答案

没有在.NET 4.5中添加索引器的PropertyInfo.SetValuePropertyInfo.GetValue重载。

但这只是将null传递给以前版本的indexer参数的问题(使用thisthis重载)。

所以:

prop.SetValue(this, value, null);




prop.GetValue(this, null);


这应该适用于.NET .3.5(最新版本)...实际上适用于NET 2.0及更高版本:-)

09-27 21:33
查看更多