blob: 0b30d6ea44fe971e9f49537b60fbd6501fca8458 (
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Mono.Cecil;
using Mono.Linker.Dataflow;
using Mono.Linker.Steps;
namespace Mono.Linker.Steps
{
// This discovers types attributed with certain serialization attributes, to match the old behavior
// of xamarin-android. It is not meant to be complete. Unlike xamarin-andorid:
// - this will only discover attributed types that are marked
// - this will discover types in non-"link" assemblies as well
public class DiscoverSerializationHandler : IMarkHandler
{
LinkContext? _context;
LinkContext Context {
get {
Debug.Assert (_context != null);
return _context;
}
}
public void Initialize (LinkContext context, MarkContext markContext)
{
_context = context;
markContext.RegisterMarkTypeAction (ProcessType);
markContext.RegisterMarkMethodAction (CheckForSerializerActivation);
}
void CheckForSerializerActivation (MethodDefinition method)
{
var type = method.DeclaringType;
if (!Context.SerializationMarker.IsActive (SerializerKind.DataContractSerializer) &&
method.IsConstructor && !method.IsStatic &&
((type.Namespace == "System.Runtime.Serialization" && type.Name == "DataContractSerializer") ||
(type.Namespace == "System.Runtime.Serialization.Json" && type.Name == "DataContractJsonSerializer"))) {
Context.SerializationMarker.Activate (SerializerKind.DataContractSerializer);
}
if (!Context.SerializationMarker.IsActive (SerializerKind.XmlSerializer) &&
method.IsConstructor && !method.IsStatic &&
type.Namespace == "System.Xml.Serialization" &&
type.Name == "XmlSerializer") {
Context.SerializationMarker.Activate (SerializerKind.XmlSerializer);
}
}
void ProcessType (TypeDefinition type)
{
ProcessAttributeProvider (type);
if (type.HasFields) {
foreach (var field in type.Fields)
ProcessAttributeProvider (field);
}
if (type.HasProperties) {
foreach (var property in type.Properties)
ProcessAttributeProvider (property);
}
if (type.HasMethods) {
foreach (var method in type.Methods)
ProcessAttributeProvider (method);
}
if (type.HasEvents) {
foreach (var @event in type.Events) {
ProcessAttributeProvider (@event);
}
}
}
void ProcessAttributeProvider (ICustomAttributeProvider provider)
{
if (!provider.HasCustomAttributes)
return;
var serializedFor = SerializerKind.None;
foreach (var attribute in provider.CustomAttributes) {
if (IsPreservedSerializationAttribute (provider, attribute, out SerializerKind serializerKind))
serializedFor |= serializerKind;
}
if (serializedFor == SerializerKind.None)
return;
if (serializedFor.HasFlag (SerializerKind.DataContractSerializer))
Context.SerializationMarker.TrackForSerialization (provider, SerializerKind.DataContractSerializer);
if (serializedFor.HasFlag (SerializerKind.XmlSerializer))
Context.SerializationMarker.TrackForSerialization (provider, SerializerKind.XmlSerializer);
}
static bool IsPreservedSerializationAttribute (ICustomAttributeProvider provider, CustomAttribute attribute, out SerializerKind serializerKind)
{
TypeReference attributeType = attribute.Constructor.DeclaringType;
serializerKind = SerializerKind.None;
switch (attributeType.Namespace) {
// http://bugzilla.xamarin.com/show_bug.cgi?id=1415
// http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx
case "System.Runtime.Serialization":
var serialized = false;
if (provider is PropertyDefinition or FieldDefinition or EventDefinition)
serialized = attributeType.Name == "DataMemberAttribute";
else if (provider is TypeDefinition)
serialized = attributeType.Name == "DataContractAttribute";
if (serialized) {
serializerKind = SerializerKind.DataContractSerializer;
return true;
}
break;
// http://msdn.microsoft.com/en-us/library/83y7df3e.aspx
case "System.Xml.Serialization":
var attributeName = attributeType.Name;
if (attributeName.StartsWith ("Xml", StringComparison.Ordinal)
&& attributeName.EndsWith ("Attribute", StringComparison.Ordinal)
&& attributeName != "XmlIgnoreAttribute") {
serializerKind = SerializerKind.XmlSerializer;
return true;
}
break;
};
return false;
}
}
}
|