-
Notifications
You must be signed in to change notification settings - Fork 524
/
RemoveRegisterAttribute.cs
65 lines (52 loc) · 1.7 KB
/
RemoveRegisterAttribute.cs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (C) 2011, Xamarin Inc.
// Copyright (C) 2010, Novell Inc.
using System;
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Mono.Cecil;
namespace Xamarin.Android.Tasks
{
public class RemoveRegisterAttribute : Task
{
const string RegisterAttribute = "Android.Runtime.RegisterAttribute";
[Required]
public ITaskItem[] ShrunkFrameworkAssemblies { get; set; }
public override bool Execute ()
{
// Find Mono.Android.dll
var mono_android = ShrunkFrameworkAssemblies.First (f => Path.GetFileNameWithoutExtension (f.ItemSpec) == "Mono.Android").ItemSpec;
using (var assembly = AssemblyDefinition.ReadAssembly (mono_android, new ReaderParameters { ReadWrite = true })) {
// Strip out [Register] attributes
foreach (TypeDefinition type in assembly.MainModule.Types)
ProcessType (type);
assembly.Write ();
}
return true;
}
private static void ProcessType (TypeDefinition type)
{
if (type.HasFields)
foreach (FieldDefinition field in type.Fields)
ProcessAttributeProvider (field);
if (type.HasMethods)
foreach (MethodDefinition method in type.Methods)
ProcessAttributeProvider (method);
}
private static void ProcessAttributeProvider (Mono.Cecil.ICustomAttributeProvider provider)
{
if (!provider.HasCustomAttributes)
return;
for (int i = 0; i < provider.CustomAttributes.Count; i++) {
if (!IsRegisterAttribute (provider.CustomAttributes [i]))
continue;
provider.CustomAttributes.RemoveAt (i--);
}
}
private static bool IsRegisterAttribute (CustomAttribute attribute)
{
return attribute.Constructor.DeclaringType.FullName == RegisterAttribute;
}
}
}