summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Build.Tasks/CompiledConverters/TypeTypeConverter.cs
blob: b8f02cc17ad88b48b88066c6c60da8d16f45c666 (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
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Xml;

using Mono.Cecil;
using Mono.Cecil.Cil;

using Xamarin.Forms.Build.Tasks;
using Xamarin.Forms.Xaml;

namespace Xamarin.Forms.Core.XamlC
{
	class TypeTypeConverter : ICompiledTypeConverter
	{
		public IEnumerable<Instruction> ConvertFromString(string value, ModuleDefinition module, BaseNode node)
		{
			if (string.IsNullOrEmpty(value))
				goto error;

			var split = value.Split(':');
			if (split.Length > 2)
				goto error;

			XmlType xmlType;
			if (split.Length == 2)
				xmlType = new XmlType(node.NamespaceResolver.LookupNamespace(split[0]), split[1], null);
			else
				xmlType = new XmlType(node.NamespaceResolver.LookupNamespace(""), split[0], null);

			var typeRef = xmlType.GetTypeReference(module, (IXmlLineInfo)node);
			if (typeRef == null)
				goto error;

			var getTypeFromHandle = module.ImportReference(typeof(Type).GetMethod("GetTypeFromHandle", new[] { typeof(RuntimeTypeHandle) }));
			yield return Instruction.Create(OpCodes.Ldtoken, module.ImportReference(typeRef));
			yield return Instruction.Create(OpCodes.Call, module.ImportReference(getTypeFromHandle));
			yield break;

		error:
			throw new XamlParseException($"Cannot convert \"{value}\" into {typeof(Type)}", node);
		}
	}

}