5

I'm trying to get TinyIoC working on Xamarin.iOS, but I'm not having a lot of luck. My project linker settings are set to "Link SDK assemblies only".

I'm literally doing something this simple:

public interface IPerson { int age { get; } }
public class Person : IPerson { public int age { get { return 99; } } }

Then my registration code looks like this (I've just placed it in my AppDelegate in a toy app):

TinyIoCContainer.Current.Register<IPerson,Person>.AsMultiInstance();

When I attempt to grab an IPerson, I get a runtime exception saying that IPerson cannot be resolved (this code is found immediately after the registration code in the AppDelegate of the toy app):

IPerson person = TinyIoCContainer.Current.Resolve<IPerson>();

Here's the error:

Unable to resolve type: TinyTest.IPerson

If, however, I change the linker settings to "Don't link", everything works fine. This is obviously untenable, though, because the binary becomes enormous.

I've tried placing [Preserve] attributes on the IPerson interface and the Person class, but no dice. I also tried just manually declaring a variable of type IPerson and instantiating it with a new Person() and then grabbing the age property, just to make sure the type was included in the build, but no luck there either.

Feel like I'm missing something here - can someone point me in the right direction?

Thank you!

1 Answers1

2

This is a bug because reflection is used to call an internal Expression<TDelegate> constructor.

The linker cannot analyze reflection usage (it's beyond static analysis) so it must be aware of those special cases.

This is obviously untenable, though, because the binary becomes enormous.

Keep using the default Link SDK option but add the --linkskip=System.Core to your Additional mtouch arguments, inside your Project Options, iOS Build.

That way only System.Core (from the SDK) will not be linked and the increase in size will be much smaller. Of course this is only a workaround until a new version fix the issue properly.

poupou
  • 43,413
  • 6
  • 77
  • 174
  • Is above syntax is correct because if writing this in mtouch argument,I am still not able to solve issue. – Mahesh Bansode May 21 '14 at 13:19
  • Not sure I'm parsing your comment correctly. In any case this issue was solved before XI 7.0 was released - so any recent version of Xamarin.iOS won't required this workaround. – poupou May 21 '14 at 13:29
  • I am asking about "linkskip" syntax is correct because I am using this for another one library. – Mahesh Bansode May 22 '14 at 09:14
  • Yes it is (correct) but your error might be different. Does it works with **Don't link** ? if so please file a bug report @ http://bugzilla.xamarin.com along with a self-contained test case and we'll have a look. – poupou May 22 '14 at 13:11
  • I came here searching for a similar issue with AutoFac, but got a different error that @poupou's answer fixed as well. Interestingly enough; i didn't have to skip linking System.Core in MonoTouch, but needed to do so in MonoDroid (I run the same code on both platforms with Link SDK). So to feed googlebot for others: I got a `Autofac.Core.DependencyResolutionException` with an inner `System.NullReferenceException` at `System.Linq.Expressions.Expression.CreateLambda`. – Johannes Rudolph Jun 03 '14 at 19:20
  • The bug should be solved at the moment I'm writing but I hit the same error: in my case the class couldn't be resolved due to a runtime error. – Daniele D. Oct 14 '15 at 09:44