System.ArgumentException: Get Method not found for 'XXXXXXX' at System.Reflection.MonoPropert

Hi,

whenever am trying to access the value of a property through reflection am getting a error like "System.ArgumentException: Get Method not found for 'XXXX' at System.Reflection.MonoProperty.Get". Am using Xamarin studio version 6.3 and property also declared as public only.

Model:

using System;
namespace XXXX.Core
{
    public class ManufacturingParameters
    {
        public ManufacturingParameters()
        {
        }

        private int oemCode;
        public int OEMCode
        {
            get { return oemCode; }
            set { oemCode = value; }
        }
    }
}

Ex Code: ValueString = deviceInfoContentVC.devInfo.manufacturingParameters.GetType().GetProperty(ParamSchema.Key).GetValue(deviceInfoContentVC.devInfo.manufacturingParameters).ToString();
The above code works perfectly in Visual studio but not in xamarin studio.

But I can access the same property through below code:
ValueString = deviceInfoContentVC.devInfo.manufacturingParameters.oemCode.ToString();

Since i wanted to access it dynamically i need reflection kind of mechanism.

Is there any alternative solution/fix for this?

Thank You.

Posts

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @PrakashShaiva

    I tried to reproduce this issue but could not when using either a simple console app or a template iOS app project. The reflection code you are using above worked on both Xamarin Studio v 6.3 and Visual Studio 2015.

    Here is the complete code I used to try to reproduce this issue. First I set up a couple of simple classes to try to replicate the code you posted as closely as possible:

    public class MyClass
    {
        public ManufacturingParameters parameters
        {
            get; set;
        }
    }
    
    public class ManufacturingParameters
    {
        private int oemCode;
        public int OEMCode
        {
            get { return oemCode; }
            set { oemCode = value; }
        }
    }
    
    public static class ParamSchema
    {
        public static string Key { get; set; }  
    }
    

    And then I did the following in the ViewDidLoad method of the iOS project (not sure what project type you are using, but VC seemed to indicate using a View Controller, so I assumed iOS)

    public partial class ViewController : UIViewController
    {
        public MyClass devInfo { get; set; }
    
        ...        
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.
            ParamSchema.Key = "OEMCode";
            var mp = new ManufacturingParameters { OEMCode = 42 };
            devInfo = new MyClass { parameters = mp };
    
            var valString = this.devInfo.parameters.GetType().GetProperty(ParamSchema.Key).GetValue(devInfo.parameters).ToString();
            Console.WriteLine($"valString: {valString}");
        }
    
        ...
    }
    

    And everything worked as expected in Xamarin Studio. Is there any more context you can give so we can reproduce the issue and get a bug report filed if need be?

  • PrakashShaivaPrakashShaiva USMember ✭✭

    @JGoldberger

    It Was the linker stripping it away. I had changed the Linker Behaviour to "Link Framework SDK's Only" from "Link All" under iOS project's settings, and under the Build heading.

    Now its works like a charm.

    Anyhow thanks for your time and patience @JGoldberger .

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    Ah, yes, "Link all" will almost always open a big can of worms. "Link Framework SDK's Only" is therecommended setting for release builds. Only use "Link All" if your app bundle is too large, but then be prepared to do some preserving of types. See these guides for more on the Linker settings:

    iOS: https://developer.xamarin.com/guides/ios/advanced_topics/linker/
    Android: https://developer.xamarin.com/guides/android/advanced_topics/linking/
    How to make a custom linker configuration for either platform: https://developer.xamarin.com/guides/cross-platform/advanced/custom_linking/

Sign In or Register to comment.