From 4feaf14c55d4062d6e52c422c3661699737de33d Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Fri, 29 Sep 2017 09:04:27 +0200 Subject: [PATCH] [XamlC] Compiled UriTypeConverter (#1166) --- .../CompiledConverters/UriTypeConverter.cs | 28 +++++++++++++++++++ .../Xamarin.Forms.Build.Tasks.csproj | 1 + Xamarin.Forms.Core/UriTypeConverter.cs | 1 + 3 files changed, 30 insertions(+) create mode 100644 Xamarin.Forms.Build.Tasks/CompiledConverters/UriTypeConverter.cs diff --git a/Xamarin.Forms.Build.Tasks/CompiledConverters/UriTypeConverter.cs b/Xamarin.Forms.Build.Tasks/CompiledConverters/UriTypeConverter.cs new file mode 100644 index 000000000..534a8bada --- /dev/null +++ b/Xamarin.Forms.Build.Tasks/CompiledConverters/UriTypeConverter.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +using Mono.Cecil; +using Mono.Cecil.Cil; +using Xamarin.Forms.Xaml; + +namespace Xamarin.Forms.Core.XamlC +{ + class UriTypeConverter : ICompiledTypeConverter + { + public IEnumerable ConvertFromString(string value, ModuleDefinition module, BaseNode node) + { + if (string.IsNullOrWhiteSpace(value)) { + yield return Instruction.Create(OpCodes.Ldnull); + yield break; + } + + var uriCtor = module.ImportReference(typeof(Uri)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 2 && md.Parameters[1].ParameterType.FullName == "System.UriKind"); + var uriCtorRef = module.ImportReference(uriCtor); + + yield return Instruction.Create(OpCodes.Ldstr, value); + yield return Instruction.Create(OpCodes.Ldc_I4_0); //UriKind.RelativeOrAbsolute + yield return Instruction.Create(OpCodes.Newobj, uriCtorRef); + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj b/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj index a4895c5c5..0560267d2 100644 --- a/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj +++ b/Xamarin.Forms.Build.Tasks/Xamarin.Forms.Build.Tasks.csproj @@ -112,6 +112,7 @@ + diff --git a/Xamarin.Forms.Core/UriTypeConverter.cs b/Xamarin.Forms.Core/UriTypeConverter.cs index 0a24cab3e..fca35705f 100644 --- a/Xamarin.Forms.Core/UriTypeConverter.cs +++ b/Xamarin.Forms.Core/UriTypeConverter.cs @@ -2,6 +2,7 @@ namespace Xamarin.Forms { + [Xaml.ProvideCompiled("Xamarin.Forms.Core.XamlC.UriTypeConverter")] public class UriTypeConverter : TypeConverter { public override object ConvertFromInvariantString(string value)