How to implement Linker in PCL with iOS?

I have created Portable Library project called SamplePL.dll with some xaml pages and refered in native iOS project. I need to create instance for xaml pages of SampleDL.dll dynamically using reflection.

I used Linker options(Linker SDK assemblies only/Linker all assemblies) when I build native iOS.

For that, I referred one of the page of SamplePL.dll as below code snippet in AppDelegate.cs FinishedLaunching method,

var obj= typeof(SamplPLXamlClass);

I used the code to create instance dynamically using reflection as below,

Activator.CreateInstance(Type.GetType("SamplePL.SampleNewXamlClass,SamplePL"))

But this shows error like

Default constructor not found in SamplePL.SampleNewXamlClass, in Activator.CreateInstance(....)

I have missed anything? or Is there any other way to implement Linker concept in PCL? If I get solution for this, it will be more helpful to reduce size of our project deliverable. Thanks in advance.

Answers

  • adamkempadamkemp USInsider, Developer Group Leader mod

    You need to use a Preserve attribute as described here. However, there is a bug in Xamarin.Android that causes the attribute to only work if you use it in the Android.Runtime namespace. This may cause problems for you if you also use the Preserve attribute in your Android project.

  • BalakrishnanGBalakrishnanG USMember

    @adamkemp Thanks for your valuable reply. I have created a PreserveAttribute class under Foundation namespace in SampleDL.dll assembly and added Preserve attribute to SamplePL.SampleNewXamlClass at class level as below,

    namespace SampleDL
    {
        [Foundation.Preserve(AllMembers = true)]
        public partial class SampleNewXamlClass : ContentPage
        {
        public SampleNewXamlClass () { }    
         }
    }
    

    One thing is I have created another PCL named SampleAppPL which only contains App.cs class and trying to create instance of SamplePL.SampleNewXamlClass dynamically in this App.cs using reflection. I have referred both PCL assemblies in native iOS.

    But its not working. Its showing same error like,

    Default constructor not found in SamplePL.SampleNewXamlClass, in Activator.CreateInstance(....)

  • adamkempadamkemp USInsider, Developer Group Leader mod

    It could be that the entire assembly is being stripped out. Inspect your .app in your bin directory and see if it contains the .dll for SamplePL. If not then your assembly wasn't included because the build system didn't see any uses of it. This step happens before the linker so the attribute (unfortunately) doesn't help.

    To fix that you would need to refer to something (anything) in that assembly from another assembly that is included. The easiest way to do that is to add a line like this in your app delegate:

    var t = typeof(SamplePL.SampleNewXamlClass);
    
  • BalakrishnanGBalakrishnanG USMember

    @adamkemp Yes, I have referred a class from SamplePL assembly in AppDelegate.cs as your code snippet but not this class (SamplePL.SampleNewXamlClass). Again I am getting same error. Actually I am creating instance using reflection in another PCL where App.cs class available. Shall I need to do further steps to fix the issue?

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Can you attach an example?

Sign In or Register to comment.