Forum Xamarin.Android

Problem using reflection in code library

PaulKeefePaulKeefe USMember ✭✭

How do I ensure linking works for reflection on anonymous types that are included in a code library? The library is pure C# so I can use it with Android and iOS but it uses reflection extensively. This works fine in the debugger but I realize now that it is not linking properly because there is no data in the reflected objects.

I can't use [Android.Runtime.Preserve(AllMembers=true)] on the class because the code library does not use the Android SDK. Is there any way to get this work?

Here's an example of what I am doing that works in the debugger:

private NameValueCollection createParams(object postData)
{
    NameValueCollection postParams = new NameValueCollection();

    postData.GetType().GetProperties().ToList()
    .ForEach(i => postParams.Add(i.Name, i.GetValue(postData, null).ToString()));

    return postParams;
}

Thanks!

Posts

  • FZelleFZelle DEMember ✭✭✭

    There is no way for the linker to know what class you are using if you only use reflection on it.

    So you either have to set the linker to SDK-Only or create a helper function that instantiates every class you use.

  • PaulKeefePaulKeefe USMember ✭✭

    I was able to get it working by setting the Linker to "None". I was under the misconception that nothing would get linked into the app but I guess it just means it won't clean up any of the code for you.

    Thanks!

Sign In or Register to comment.