3

I want to convert DataRow To Object. I write 1 class to do this. Error like this:

No overload for method 'SetValue' takes 2 arguments

No overload for method 'GetValue' takes 1 arguments

But I can't using GetValues() and SetValues(). When converting project to 4.5. It's work. My project is set platform target is 3.5 (Obligatory - because I connect with the device must using .NET 3.5).

How to fix this?

Here my code:

    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;
    }
  • http://stackoverflow.com/questions/3436526/detect-target-framework-version-at-compile-time – Bharadwaj Dec 14 '15 at 07:16
  • @Bharadwaj: How is that relevant? – Jon Skeet Dec 14 '15 at 07:17
  • @JonSkeet OP is using 4.5, but want to deploy for 3.5, was that link won't help in that way? – Bharadwaj Dec 14 '15 at 07:18
  • @Bharadwaj no, not at all. Have you actually read the question? – Jcl Dec 14 '15 at 07:19
  • @Jcl hmm, might be I understood in wrong way, can you just explain what exactly OP wants? I commented as I understood – Bharadwaj Dec 14 '15 at 07:19
  • @Bharadwaj: Nope. There's no need to use conditional compilation here... there's not even any evidence that the OP needs to compile targeting two different frameworks, only 3.5. – Jon Skeet Dec 14 '15 at 07:20
  • @Bharadwaj his problem is that the overload he is using is only on .NET 4.5 and up, and was not in previous versions, so he's trying to find the equivalent for .NET 3.5... nothing to do with deploying for different versions – Jcl Dec 14 '15 at 07:20
  • @JonSkeet then why did OP mentioned 4.5 if it is not relevant? – Bharadwaj Dec 14 '15 at 07:21
  • @Bharadwaj: Because it worked when they *did* target 4.5. Now they need to target 3.5 instead. Seems reasonable to me, but there's no indication that they need to target both at the same time. – Jon Skeet Dec 14 '15 at 07:22
  • @JonSkeet I still didn't got it. If OP changed to 3.5, then OP could have gone through the document for it – Bharadwaj Dec 14 '15 at 07:25
  • @Bharadwaj: But it wouldn't have helped them IMO. Whereas using an overload available in 3.5 (and 4.5) does. – Jon Skeet Dec 14 '15 at 07:27
  • @JonSkeet So the question confused for me. I didn't understood the what OP actually wants. – Bharadwaj Dec 14 '15 at 07:29
  • @Bharadwaj: The OP wants to call `PropertyInfo.GetValue` and `PropertyInfo.SetValue`. Their code worked when targeting .NET 4.5, but doesn't work when targeting .NET 3.5, because the overload they were using was only introduced in .NET 4.5. The solution is just to use a different overload. – Jon Skeet Dec 14 '15 at 08:09

1 Answers1

9

There is an overload for PropertyInfo.SetValue and PropertyInfo.GetValue without the indexer added in .NET 4.5.

But it's simply a matter of passing null to the indexer parameter on previous versions (using this and this overloads).

So:

prop.SetValue(this, value, null);

And

prop.GetValue(this, null);

This should work on .NET .3.5 (up to recent versions)... actually for NET 2.0 and up :-)

Jcl
  • 27,696
  • 5
  • 61
  • 92