summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Build.Tasks/CompiledConverters/LayoutOptionsConverter.cs
blob: e252ed9d19601866e895f5d9979f845823c23c3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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 LayoutOptionsConverter : ICompiledTypeConverter
	{
		public IEnumerable<Instruction> ConvertFromString(string value, ModuleDefinition module, BaseNode node)
		{
			do {
				if (string.IsNullOrEmpty(value))
					break;

				value = value.Trim();

				var parts = value.Split('.');
				if (parts.Length == 1 || (parts.Length == 2 && parts [0] == "LayoutOptions")) {
					var options = parts [parts.Length - 1];

					var field = module.Import(typeof(LayoutOptions)).Resolve().Fields.SingleOrDefault(fd => fd.Name == options && fd.IsStatic);
					if (field != null) {
						yield return Instruction.Create(OpCodes.Ldsfld, module.Import(field));
						yield break;
					}
				}
			} while (false);

			throw new XamlParseException(String.Format("Cannot convert \"{0}\" into {1}", value, typeof(LayoutOptions)), node);
		}
	}
}