0

I have the below code in my method (actually an async static Task)

try
{
    string responseValues = JSONtoKeyValue(responseBody);
    Console.WriteLine("responseValues = " + responseValues);

    //var dict = responseValues.Split('|').Select(x => x.Split('=')).ToDictionary(x => x[0], x => x[1]);
    var dict = responseValues.Split('|')
        .Select(x => x.Split('='))
        .Where(x => x.Length > 1 && !String.IsNullOrEmpty(x[0].Trim())
        && !String.IsNullOrEmpty(x[1].Trim()))
        .ToDictionary(x => x[0].Trim(), x => x[1].Trim());

    Console.WriteLine("Dictionary created.");
    Console.ReadLine();

    foreach (KeyValuePair<string, string> entry in dict)
    {
        if (entry.Value == null)
        {
            dict.Remove(entry.Key);
        }
        else
        {
            string key = entry.Key;
            string value = entry.Value;

            if (cnsnt.GetType().GetTypeInfo().GetDeclaredProperty(key) != null)
            {
                cnsnt.GetType().GetTypeInfo().GetDeclaredProperty(key).SetValue(value);
            }
        }
    }
}
catch (System.Exception e)
{
    Console.WriteLine(e.Message);
}

My problem is two-fold. First, I can't handle the duplicates for the dictionary (wanted to just keep the latest value if the key already exists) but even when I try with no duplicates, when I try to see if I have the property that the key has, I get error No overload for method SetValue has only one argument.

Any help would be appreciated immensely ..

Nick
  • 483
  • 1
  • 6
  • 15
  • We need to know what `cnsnt` is if that is the line throwing the error. –  Jan 09 '20 at 18:54
  • 1
    `SetValue` has no overload that only takes one argument. I'm not sure what you think the problem is. Look at the docs for SetValue https://learn.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.setvalue?view=netframework-4.8 there is no overload with only one parameter. –  Jan 09 '20 at 18:57
  • 1
    why are you trying to use reflection here? – Asım Gündüz Jan 09 '20 at 18:57
  • Yeah its really confusing why you're using reflection and `SetValue` at all. You might want to look up a tutorial on how to work with Dictionarys in C#. Its a lot simpler than you are making it out to be. –  Jan 09 '20 at 18:58
  • 1
    SetValue method needs two arguments. An object of the class for which you want to set the property and the value of the property. [Read here](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.setvalue?view=netframework-4.8#System_Reflection_PropertyInfo_SetValue_System_Object_System_Object_). Your need to call SetValue as `SetValue(cnsnt, value)` – Chetan Jan 09 '20 at 19:00
  • @Josh: thank you for looking it up, it's in Program class as is my Task and is just a class with only properties, 'public static PropSelf` with a bunch of properties like `public string userName { get; set; }` etc, very simple. Inside my Program class I declare in the beginning `public static PropSelf cnsnt = new PropSelf();` and that's it, I populate some of the properties with values, the task runs smoothly, returns the response, which I capture and I transform to a pipe-separated string. – Nick Jan 09 '20 at 19:01
  • @Josh, thank you for pointing out the one argument, you are right, takes object and string; can't go further on testing because of duplicate values in the dictionary - thank you! – Nick Jan 09 '20 at 19:04
  • @Chetan Ranpariya: thank you very much, also Josh pointed out.. silly mistake, can't check the validity though because of the duplicate values in the dict.. – Nick Jan 09 '20 at 19:05
  • Could I change the `var dict...` to avoid duplicates by keeping the latest value in case the key is already in the dictionary? – Nick Jan 09 '20 at 19:06
  • You can not have two values for the same key in dictionary. What exactly you are trying to achieve? You are checking if the property has the value and if not you are setting the value? From where you might get the duplicate values in dictionary? – Chetan Jan 09 '20 at 19:16
  • @Nick When you assign a value to a key in a dictionary that already exists, the existing value gets replaced with the new one. You cannot store more than one value per key, it is 1:1. If it stored multiple values for one key it would be a Hash Map. –  Jan 09 '20 at 20:31
  • @Josh: indeed, but, the var.. tries to enter a key that already exists, and this produces error (the catch is being activated).. You may check with a string by replacing the responseValues, like: `string responseValues = "some|nice|value|here|and|here"` - it produces error key already exists, so I can't go on with looping the properties.. – Nick Jan 09 '20 at 21:10

1 Answers1

1

Check the https://learn.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.setvalue?view=netframework-4.8 set value documentation. There is indeed no such overload with one parameter.

Here is the easy way to update the dictionary value How to update the value stored in Dictionary in C#?

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • thank you very much indeed.. problem is that inside that string are duplicate values, and I need to edit the `var dict = responseValues.Split('|')...` somehow, to keep the latest value if the key already exists.. thank you also for the links. – Nick Jan 09 '20 at 19:08