none
I have an issue. I am trying to set a property using reflection. RRS feed

  • Question

  •    I have an issue. I am trying to set a property using reflection.
    This is using .netstandard 2.0. I get the value just fine
    but when I attempt to set it I get the error listed. They types do match
    so I don't know what the issue is.

    Code:
            private void ReadProperties()
            {
                BindingFlags flags = BindingFlags.Public |
                                     //BindingFlags.NonPublic |
                                     BindingFlags.FlattenHierarchy |
                                     BindingFlags.Instance;

                //
                // get all public properties and Put them here
                //
                PropertyInfo[] propertyInfos = GetType().GetProperties(flags);
                foreach (PropertyInfo propInfo in propertyInfos)
                {
                    //
                    // if we have a matching property then
                    // we should copy it to our property
                    //
                    if (null == propInfo) continue;
                    if (null == propInfo.GetSetMethod()) continue;

                    if (Application.Current.Properties.ContainsKey(propInfo.Name))
                    {
                        try
                        {
                            object data = Application.Current.Properties[propInfo.Name];
                            if (null != data)
                            {
                                if (data.GetType() == propInfo.PropertyType)
                                {
    line 91                       propInfo.SetValue(propInfo, data, null);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                        }
                    }
                    break;
                }

            }

    Error: (Stacktrace)
      at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
       at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
       at TestPce.ViewModel.SettingsPageViewModel.ReadProperties() in C:\Projects\CrossPlatform\TestPce - HAL\TestPce\TestPce\ViewModel\SettingsPageViewModel.cs:line 91


    A Question


    • Edited by hglenni Wednesday, November 1, 2017 1:18 PM
    • Moved by Mattew Wu Friday, November 3, 2017 1:55 AM not app studio issue
    Wednesday, November 1, 2017 1:17 PM

Answers