~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/Newtonsoft.Json/Src/Newtonsoft.Json/Serialization/JsonTypeReflector.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#region License
2
 
// Copyright (c) 2007 James Newton-King
3
 
//
4
 
// Permission is hereby granted, free of charge, to any person
5
 
// obtaining a copy of this software and associated documentation
6
 
// files (the "Software"), to deal in the Software without
7
 
// restriction, including without limitation the rights to use,
8
 
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 
// copies of the Software, and to permit persons to whom the
10
 
// Software is furnished to do so, subject to the following
11
 
// conditions:
12
 
//
13
 
// The above copyright notice and this permission notice shall be
14
 
// included in all copies or substantial portions of the Software.
15
 
//
16
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 
// OTHER DEALINGS IN THE SOFTWARE.
24
 
#endregion
25
 
 
26
 
using System;
27
 
using System.ComponentModel;
28
 
using System.Globalization;
29
 
using System.Linq;
30
 
using System.Reflection;
31
 
using System.Runtime.Serialization;
32
 
using System.Security.Permissions;
33
 
using Newtonsoft.Json.Utilities;
34
 
 
35
 
namespace Newtonsoft.Json.Serialization
36
 
{
37
 
#if !SILVERLIGHT && !PocketPC && !NET20
38
 
  internal interface IMetadataTypeAttribute
39
 
  {
40
 
    Type MetadataClassType { get; }
41
 
  }
42
 
#endif
43
 
 
44
 
  internal static class JsonTypeReflector
45
 
  {
46
 
    public const string IdPropertyName = "$id";
47
 
    public const string RefPropertyName = "$ref";
48
 
    public const string TypePropertyName = "$type";
49
 
    public const string ArrayValuesPropertyName = "$values";
50
 
 
51
 
    public const string ShouldSerializePrefix = "ShouldSerialize";
52
 
 
53
 
    private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> JsonConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetJsonConverterTypeFromAttribute);
54
 
#if !SILVERLIGHT && !PocketPC && !NET20
55
 
    private static readonly ThreadSafeStore<Type, Type> AssociatedMetadataTypesCache = new ThreadSafeStore<Type, Type>(GetAssociateMetadataTypeFromAttribute);
56
 
 
57
 
    private const string MetadataTypeAttributeTypeName =
58
 
      "System.ComponentModel.DataAnnotations.MetadataTypeAttribute, System.ComponentModel.DataAnnotations, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
59
 
    private static Type _cachedMetadataTypeAttributeType;
60
 
#endif
61
 
#if SILVERLIGHT
62
 
    private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> TypeConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetTypeConverterTypeFromAttribute);
63
 
 
64
 
    private static Type GetTypeConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
65
 
    {
66
 
      TypeConverterAttribute converterAttribute = GetAttribute<TypeConverterAttribute>(attributeProvider);
67
 
      if (converterAttribute == null)
68
 
        return null;
69
 
 
70
 
      return Type.GetType(converterAttribute.ConverterTypeName);
71
 
    }
72
 
 
73
 
    private static Type GetTypeConverterType(ICustomAttributeProvider attributeProvider)
74
 
    {
75
 
      return TypeConverterTypeCache.Get(attributeProvider);
76
 
    }
77
 
#endif
78
 
 
79
 
    public static JsonContainerAttribute GetJsonContainerAttribute(Type type)
80
 
    {
81
 
      return CachedAttributeGetter<JsonContainerAttribute>.GetAttribute(type);
82
 
    }
83
 
 
84
 
    public static JsonObjectAttribute GetJsonObjectAttribute(Type type)
85
 
    {
86
 
      return GetJsonContainerAttribute(type) as JsonObjectAttribute;
87
 
    }
88
 
 
89
 
    public static JsonArrayAttribute GetJsonArrayAttribute(Type type)
90
 
    {
91
 
      return GetJsonContainerAttribute(type) as JsonArrayAttribute;
92
 
    }
93
 
 
94
 
#if !PocketPC && !NET20
95
 
    public static DataContractAttribute GetDataContractAttribute(Type type)
96
 
    {
97
 
      return CachedAttributeGetter<DataContractAttribute>.GetAttribute(type);
98
 
    }
99
 
#endif
100
 
 
101
 
    public static MemberSerialization GetObjectMemberSerialization(Type objectType)
102
 
    {
103
 
      JsonObjectAttribute objectAttribute = GetJsonObjectAttribute(objectType);
104
 
 
105
 
      if (objectAttribute == null)
106
 
      {
107
 
#if !PocketPC && !NET20
108
 
        DataContractAttribute dataContractAttribute = GetDataContractAttribute(objectType);
109
 
 
110
 
        if (dataContractAttribute != null)
111
 
          return MemberSerialization.OptIn;
112
 
#endif
113
 
 
114
 
        return MemberSerialization.OptOut;
115
 
      }
116
 
 
117
 
      return objectAttribute.MemberSerialization;
118
 
    }
119
 
 
120
 
    private static Type GetJsonConverterType(ICustomAttributeProvider attributeProvider)
121
 
    {
122
 
      return JsonConverterTypeCache.Get(attributeProvider);
123
 
    }
124
 
 
125
 
    private static Type GetJsonConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
126
 
    {
127
 
      JsonConverterAttribute converterAttribute = GetAttribute<JsonConverterAttribute>(attributeProvider);
128
 
      return (converterAttribute != null)
129
 
        ? converterAttribute.ConverterType
130
 
        : null;
131
 
    }
132
 
 
133
 
    public static JsonConverter GetJsonConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType)
134
 
    {
135
 
      Type converterType = GetJsonConverterType(attributeProvider);
136
 
 
137
 
      if (converterType != null)
138
 
      {
139
 
        JsonConverter memberConverter = JsonConverterAttribute.CreateJsonConverterInstance(converterType);
140
 
 
141
 
        if (!memberConverter.CanConvert(targetConvertedType))
142
 
          throw new JsonSerializationException("JsonConverter {0} on {1} is not compatible with member type {2}.".FormatWith(CultureInfo.InvariantCulture, memberConverter.GetType().Name, attributeProvider, targetConvertedType.Name));
143
 
 
144
 
        return memberConverter;
145
 
      }
146
 
 
147
 
      return null;
148
 
    }
149
 
 
150
 
#if !PocketPC
151
 
    public static TypeConverter GetTypeConverter(Type type)
152
 
    {
153
 
#if !SILVERLIGHT
154
 
      return TypeDescriptor.GetConverter(type);
155
 
#else
156
 
      Type converterType = GetTypeConverterType(type);
157
 
 
158
 
      if (converterType != null)
159
 
        return (TypeConverter)ReflectionUtils.CreateInstance(converterType);
160
 
 
161
 
      return null;
162
 
#endif
163
 
    }
164
 
#endif
165
 
 
166
 
#if !SILVERLIGHT && !PocketPC && !NET20
167
 
    private static Type GetAssociatedMetadataType(Type type)
168
 
    {
169
 
      return AssociatedMetadataTypesCache.Get(type);
170
 
    }
171
 
 
172
 
    private static Type GetAssociateMetadataTypeFromAttribute(Type type)
173
 
    {
174
 
      Type metadataTypeAttributeType = GetMetadataTypeAttributeType();
175
 
      if (metadataTypeAttributeType == null)
176
 
        return null;
177
 
 
178
 
      object attribute = type.GetCustomAttributes(metadataTypeAttributeType, true).SingleOrDefault();
179
 
      if (attribute == null)
180
 
        return null;
181
 
 
182
 
      IMetadataTypeAttribute metadataTypeAttribute = (DynamicCodeGeneration)
183
 
                                                       ? DynamicWrapper.CreateWrapper<IMetadataTypeAttribute>(attribute)
184
 
                                                       : new LateBoundMetadataTypeAttribute(attribute);
185
 
 
186
 
      return metadataTypeAttribute.MetadataClassType;
187
 
    }
188
 
 
189
 
    private static Type GetMetadataTypeAttributeType()
190
 
    {
191
 
      // always attempt to get the metadata type attribute type
192
 
      // the assembly may have been loaded since last time
193
 
      if (_cachedMetadataTypeAttributeType == null)
194
 
      {
195
 
        Type metadataTypeAttributeType = Type.GetType(MetadataTypeAttributeTypeName);
196
 
 
197
 
        if (metadataTypeAttributeType != null)
198
 
          _cachedMetadataTypeAttributeType = metadataTypeAttributeType;
199
 
        else
200
 
          return null;
201
 
      }
202
 
      
203
 
      return _cachedMetadataTypeAttributeType;
204
 
    }
205
 
 
206
 
    private static T GetAttribute<T>(Type type) where T : Attribute
207
 
    {
208
 
      Type metadataType = GetAssociatedMetadataType(type);
209
 
      if (metadataType != null)
210
 
      {
211
 
        T attribute = ReflectionUtils.GetAttribute<T>(metadataType, true);
212
 
        if (attribute != null)
213
 
          return attribute;
214
 
      }
215
 
 
216
 
      return ReflectionUtils.GetAttribute<T>(type, true);
217
 
    }
218
 
 
219
 
    private static T GetAttribute<T>(MemberInfo memberInfo) where T : Attribute
220
 
    {
221
 
      Type metadataType = GetAssociatedMetadataType(memberInfo.DeclaringType);
222
 
      if (metadataType != null)
223
 
      {
224
 
        MemberInfo metadataTypeMemberInfo = metadataType.GetMember(memberInfo.Name,
225
 
          memberInfo.MemberType,
226
 
          BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance).SingleOrDefault();
227
 
 
228
 
        if (metadataTypeMemberInfo != null)
229
 
        {
230
 
          T attribute = ReflectionUtils.GetAttribute<T>(metadataTypeMemberInfo, true);
231
 
          if (attribute != null)
232
 
            return attribute;
233
 
        }
234
 
      }
235
 
 
236
 
      return ReflectionUtils.GetAttribute<T>(memberInfo, true);
237
 
    }
238
 
 
239
 
    public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider) where T : Attribute
240
 
    {
241
 
      Type type = attributeProvider as Type;
242
 
      if (type != null)
243
 
        return GetAttribute<T>(type);
244
 
 
245
 
      MemberInfo memberInfo = attributeProvider as MemberInfo;
246
 
      if (memberInfo != null)
247
 
        return GetAttribute<T>(memberInfo);
248
 
 
249
 
      return ReflectionUtils.GetAttribute<T>(attributeProvider, true);
250
 
    }
251
 
#else
252
 
    public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider) where T : Attribute
253
 
    {
254
 
      return ReflectionUtils.GetAttribute<T>(attributeProvider, true);
255
 
    }
256
 
#endif
257
 
 
258
 
    private static bool? _dynamicCodeGeneration;
259
 
 
260
 
    public static bool DynamicCodeGeneration
261
 
    {
262
 
      get
263
 
      {
264
 
        if (_dynamicCodeGeneration == null)
265
 
        {
266
 
#if !PocketPC && !SILVERLIGHT
267
 
          try
268
 
          {
269
 
            new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand();
270
 
            new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess).Demand();
271
 
            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
272
 
            _dynamicCodeGeneration = true;
273
 
          }
274
 
          catch (Exception)
275
 
          {
276
 
            _dynamicCodeGeneration = false;
277
 
          }
278
 
#else
279
 
          _dynamicCodeGeneration = false;
280
 
#endif
281
 
        }
282
 
 
283
 
        return _dynamicCodeGeneration.Value;
284
 
      }
285
 
    }
286
 
 
287
 
    public static ReflectionDelegateFactory ReflectionDelegateFactory
288
 
    {
289
 
      get
290
 
      {
291
 
#if !PocketPC && !SILVERLIGHT
292
 
        if (DynamicCodeGeneration)
293
 
          return DynamicReflectionDelegateFactory.Instance;
294
 
#endif
295
 
 
296
 
        return LateBoundReflectionDelegateFactory.Instance;
297
 
      }
298
 
    }
299
 
  }
 
1
#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
4
// Permission is hereby granted, free of charge, to any person
 
5
// obtaining a copy of this software and associated documentation
 
6
// files (the "Software"), to deal in the Software without
 
7
// restriction, including without limitation the rights to use,
 
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the
 
10
// Software is furnished to do so, subject to the following
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
23
// OTHER DEALINGS IN THE SOFTWARE.
 
24
#endregion
 
25
 
 
26
using System;
 
27
using System.ComponentModel;
 
28
using System.Globalization;
 
29
using System.Reflection;
 
30
#if !(NETFX_CORE || PORTABLE)
 
31
using System.Security.Permissions;
 
32
#endif
 
33
using Newtonsoft.Json.Utilities;
 
34
#if NETFX_CORE || PORTABLE
 
35
using ICustomAttributeProvider = Newtonsoft.Json.Utilities.CustomAttributeProvider;
 
36
#endif
 
37
#if NET20
 
38
using Newtonsoft.Json.Utilities.LinqBridge;
 
39
#else
 
40
using System.Linq;
 
41
#endif
 
42
using System.Runtime.Serialization;
 
43
 
 
44
namespace Newtonsoft.Json.Serialization
 
45
{
 
46
#if !SILVERLIGHT && !PocketPC && !NET20 && !NETFX_CORE
 
47
  internal interface IMetadataTypeAttribute
 
48
  {
 
49
    Type MetadataClassType { get; }
 
50
  }
 
51
#endif
 
52
 
 
53
  internal static class JsonTypeReflector
 
54
  {
 
55
    public const string IdPropertyName = "$id";
 
56
    public const string RefPropertyName = "$ref";
 
57
    public const string TypePropertyName = "$type";
 
58
    public const string ValuePropertyName = "$value";
 
59
    public const string ArrayValuesPropertyName = "$values";
 
60
 
 
61
    public const string ShouldSerializePrefix = "ShouldSerialize";
 
62
    public const string SpecifiedPostfix = "Specified";
 
63
 
 
64
    private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> JsonConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetJsonConverterTypeFromAttribute);
 
65
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
 
66
    private static readonly ThreadSafeStore<Type, Type> AssociatedMetadataTypesCache = new ThreadSafeStore<Type, Type>(GetAssociateMetadataTypeFromAttribute);
 
67
 
 
68
    private const string MetadataTypeAttributeTypeName =
 
69
      "System.ComponentModel.DataAnnotations.MetadataTypeAttribute, System.ComponentModel.DataAnnotations, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
 
70
    private static Type _cachedMetadataTypeAttributeType;
 
71
#endif
 
72
#if SILVERLIGHT
 
73
    private static readonly ThreadSafeStore<ICustomAttributeProvider, Type> TypeConverterTypeCache = new ThreadSafeStore<ICustomAttributeProvider, Type>(GetTypeConverterTypeFromAttribute);
 
74
 
 
75
    private static Type GetTypeConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
 
76
    {
 
77
      TypeConverterAttribute converterAttribute = GetAttribute<TypeConverterAttribute>(attributeProvider);
 
78
      if (converterAttribute == null)
 
79
        return null;
 
80
 
 
81
      return Type.GetType(converterAttribute.ConverterTypeName);
 
82
    }
 
83
 
 
84
    private static Type GetTypeConverterType(ICustomAttributeProvider attributeProvider)
 
85
    {
 
86
      return TypeConverterTypeCache.Get(attributeProvider);
 
87
    }
 
88
#endif
 
89
 
 
90
    public static JsonContainerAttribute GetJsonContainerAttribute(Type type)
 
91
    {
 
92
      return CachedAttributeGetter<JsonContainerAttribute>.GetAttribute(type.GetCustomAttributeProvider());
 
93
    }
 
94
 
 
95
    public static JsonObjectAttribute GetJsonObjectAttribute(Type type)
 
96
    {
 
97
      return GetJsonContainerAttribute(type) as JsonObjectAttribute;
 
98
    }
 
99
 
 
100
    public static JsonArrayAttribute GetJsonArrayAttribute(Type type)
 
101
    {
 
102
      return GetJsonContainerAttribute(type) as JsonArrayAttribute;
 
103
    }
 
104
 
 
105
    public static JsonDictionaryAttribute GetJsonDictionaryAttribute(Type type)
 
106
    {
 
107
      return GetJsonContainerAttribute(type) as JsonDictionaryAttribute;
 
108
    }
 
109
 
 
110
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
111
    public static SerializableAttribute GetSerializableAttribute(Type type)
 
112
    {
 
113
      return CachedAttributeGetter<SerializableAttribute>.GetAttribute(type.GetCustomAttributeProvider());
 
114
    }
 
115
#endif
 
116
 
 
117
#if !PocketPC && !NET20
 
118
    public static DataContractAttribute GetDataContractAttribute(Type type)
 
119
    {
 
120
      // DataContractAttribute does not have inheritance
 
121
      Type currentType = type;
 
122
 
 
123
      while (currentType != null)
 
124
      {
 
125
        DataContractAttribute result = CachedAttributeGetter<DataContractAttribute>.GetAttribute(currentType.GetCustomAttributeProvider());
 
126
        if (result != null)
 
127
          return result;
 
128
 
 
129
        currentType = currentType.BaseType();
 
130
      }
 
131
 
 
132
      return null;
 
133
    }
 
134
 
 
135
    public static DataMemberAttribute GetDataMemberAttribute(MemberInfo memberInfo)
 
136
    {
 
137
      // DataMemberAttribute does not have inheritance
 
138
 
 
139
      // can't override a field
 
140
      if (memberInfo.MemberType() == MemberTypes.Field)
 
141
        return CachedAttributeGetter<DataMemberAttribute>.GetAttribute(memberInfo.GetCustomAttributeProvider());
 
142
 
 
143
      // search property and then search base properties if nothing is returned and the property is virtual
 
144
      PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
 
145
      DataMemberAttribute result = CachedAttributeGetter<DataMemberAttribute>.GetAttribute(propertyInfo.GetCustomAttributeProvider());
 
146
      if (result == null)
 
147
      {
 
148
        if (propertyInfo.IsVirtual())
 
149
        {
 
150
          Type currentType = propertyInfo.DeclaringType;
 
151
 
 
152
          while (result == null && currentType != null)
 
153
          {
 
154
            PropertyInfo baseProperty = (PropertyInfo)ReflectionUtils.GetMemberInfoFromType(currentType, propertyInfo);
 
155
            if (baseProperty != null && baseProperty.IsVirtual())
 
156
              result = CachedAttributeGetter<DataMemberAttribute>.GetAttribute(baseProperty.GetCustomAttributeProvider());
 
157
 
 
158
            currentType = currentType.BaseType();
 
159
          }
 
160
        }
 
161
      }
 
162
 
 
163
      return result;
 
164
    }
 
165
#endif
 
166
 
 
167
    public static MemberSerialization GetObjectMemberSerialization(Type objectType, bool ignoreSerializableAttribute)
 
168
    {
 
169
      JsonObjectAttribute objectAttribute = GetJsonObjectAttribute(objectType);
 
170
      if (objectAttribute != null)
 
171
        return objectAttribute.MemberSerialization;
 
172
 
 
173
#if !PocketPC && !NET20
 
174
      DataContractAttribute dataContractAttribute = GetDataContractAttribute(objectType);
 
175
      if (dataContractAttribute != null)
 
176
        return MemberSerialization.OptIn;
 
177
#endif
 
178
 
 
179
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
180
      if (!ignoreSerializableAttribute)
 
181
      {
 
182
        SerializableAttribute serializableAttribute = GetSerializableAttribute(objectType);
 
183
        if (serializableAttribute != null)
 
184
          return MemberSerialization.Fields;
 
185
      }
 
186
#endif
 
187
 
 
188
      // the default
 
189
      return MemberSerialization.OptOut;
 
190
    }
 
191
 
 
192
    private static Type GetJsonConverterType(ICustomAttributeProvider attributeProvider)
 
193
    {
 
194
      return JsonConverterTypeCache.Get(attributeProvider);
 
195
    }
 
196
 
 
197
    private static Type GetJsonConverterTypeFromAttribute(ICustomAttributeProvider attributeProvider)
 
198
    {
 
199
      JsonConverterAttribute converterAttribute = GetAttribute<JsonConverterAttribute>(attributeProvider);
 
200
      return (converterAttribute != null)
 
201
        ? converterAttribute.ConverterType
 
202
        : null;
 
203
    }
 
204
 
 
205
    public static JsonConverter GetJsonConverter(ICustomAttributeProvider attributeProvider, Type targetConvertedType)
 
206
    {
 
207
      object provider = null;
 
208
#if !(NETFX_CORE || PORTABLE)
 
209
      provider = attributeProvider as MemberInfo;
 
210
#else
 
211
      provider = attributeProvider.UnderlyingObject;
 
212
#endif
 
213
 
 
214
      Type converterType = GetJsonConverterType(attributeProvider);
 
215
 
 
216
      if (converterType != null)
 
217
      {
 
218
        JsonConverter memberConverter = JsonConverterAttribute.CreateJsonConverterInstance(converterType);
 
219
 
 
220
        return memberConverter;
 
221
      }
 
222
 
 
223
      return null;
 
224
    }
 
225
 
 
226
#if !(NETFX_CORE || PORTABLE)
 
227
#if !PocketPC
 
228
    public static TypeConverter GetTypeConverter(Type type)
 
229
    {
 
230
#if !SILVERLIGHT
 
231
      return TypeDescriptor.GetConverter(type);
 
232
#else
 
233
      Type converterType = GetTypeConverterType(type);
 
234
 
 
235
      if (converterType != null)
 
236
        return (TypeConverter)ReflectionUtils.CreateInstance(converterType);
 
237
 
 
238
      return null;
 
239
#endif
 
240
#endif
 
241
    }
 
242
#endif
 
243
 
 
244
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
 
245
    private static Type GetAssociatedMetadataType(Type type)
 
246
    {
 
247
      return AssociatedMetadataTypesCache.Get(type);
 
248
    }
 
249
 
 
250
    private static Type GetAssociateMetadataTypeFromAttribute(Type type)
 
251
    {
 
252
      Type metadataTypeAttributeType = GetMetadataTypeAttributeType();
 
253
      if (metadataTypeAttributeType == null)
 
254
        return null;
 
255
 
 
256
      object attribute = type.GetCustomAttributes(metadataTypeAttributeType, true).SingleOrDefault();
 
257
      if (attribute == null)
 
258
        return null;
 
259
 
 
260
      IMetadataTypeAttribute metadataTypeAttribute = (DynamicCodeGeneration)
 
261
                                                       ? DynamicWrapper.CreateWrapper<IMetadataTypeAttribute>(attribute)
 
262
                                                       : new LateBoundMetadataTypeAttribute(attribute);
 
263
 
 
264
      return metadataTypeAttribute.MetadataClassType;
 
265
    }
 
266
 
 
267
    private static Type GetMetadataTypeAttributeType()
 
268
    {
 
269
      // always attempt to get the metadata type attribute type
 
270
      // the assembly may have been loaded since last time
 
271
      if (_cachedMetadataTypeAttributeType == null)
 
272
      {
 
273
        Type metadataTypeAttributeType = Type.GetType(MetadataTypeAttributeTypeName);
 
274
 
 
275
        if (metadataTypeAttributeType != null)
 
276
          _cachedMetadataTypeAttributeType = metadataTypeAttributeType;
 
277
        else
 
278
          return null;
 
279
      }
 
280
 
 
281
      return _cachedMetadataTypeAttributeType;
 
282
    }
 
283
#endif
 
284
 
 
285
    private static T GetAttribute<T>(Type type) where T : Attribute
 
286
    {
 
287
      T attribute;
 
288
 
 
289
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
 
290
      Type metadataType = GetAssociatedMetadataType(type);
 
291
      if (metadataType != null)
 
292
      {
 
293
        attribute = ReflectionUtils.GetAttribute<T>(metadataType, true);
 
294
        if (attribute != null)
 
295
          return attribute;
 
296
      }
 
297
#endif
 
298
 
 
299
      attribute = ReflectionUtils.GetAttribute<T>(type.GetCustomAttributeProvider(), true);
 
300
      if (attribute != null)
 
301
        return attribute;
 
302
 
 
303
      foreach (Type typeInterface in type.GetInterfaces())
 
304
      {
 
305
        attribute = ReflectionUtils.GetAttribute<T>(typeInterface.GetCustomAttributeProvider(), true);
 
306
        if (attribute != null)
 
307
          return attribute;
 
308
      }
 
309
 
 
310
      return null;
 
311
    }
 
312
 
 
313
    private static T GetAttribute<T>(MemberInfo memberInfo) where T : Attribute
 
314
    {
 
315
      T attribute;
 
316
 
 
317
#if !(SILVERLIGHT || NET20 || NETFX_CORE || PORTABLE)
 
318
      Type metadataType = GetAssociatedMetadataType(memberInfo.DeclaringType);
 
319
      if (metadataType != null)
 
320
      {
 
321
        MemberInfo metadataTypeMemberInfo = ReflectionUtils.GetMemberInfoFromType(metadataType, memberInfo);
 
322
 
 
323
        if (metadataTypeMemberInfo != null)
 
324
        {
 
325
          attribute = ReflectionUtils.GetAttribute<T>(metadataTypeMemberInfo, true);
 
326
          if (attribute != null)
 
327
            return attribute;
 
328
        }
 
329
      }
 
330
#endif
 
331
 
 
332
      attribute = ReflectionUtils.GetAttribute<T>(memberInfo.GetCustomAttributeProvider(), true);
 
333
      if (attribute != null)
 
334
        return attribute;
 
335
 
 
336
      if (memberInfo.DeclaringType != null)
 
337
      {
 
338
        foreach (Type typeInterface in memberInfo.DeclaringType.GetInterfaces())
 
339
        {
 
340
          MemberInfo interfaceTypeMemberInfo = ReflectionUtils.GetMemberInfoFromType(typeInterface, memberInfo);
 
341
 
 
342
          if (interfaceTypeMemberInfo != null)
 
343
          {
 
344
            attribute = ReflectionUtils.GetAttribute<T>(interfaceTypeMemberInfo.GetCustomAttributeProvider(), true);
 
345
            if (attribute != null)
 
346
              return attribute;
 
347
          }
 
348
        }
 
349
      }
 
350
 
 
351
      return null;
 
352
    }
 
353
 
 
354
    public static T GetAttribute<T>(ICustomAttributeProvider attributeProvider) where T : Attribute
 
355
    {
 
356
      object provider = null;
 
357
#if !(NETFX_CORE || PORTABLE)
 
358
      provider = attributeProvider;
 
359
#else
 
360
      provider = attributeProvider.UnderlyingObject;
 
361
#endif
 
362
 
 
363
      Type type = provider as Type;
 
364
      if (type != null)
 
365
        return GetAttribute<T>(type);
 
366
 
 
367
      MemberInfo memberInfo = provider as MemberInfo;
 
368
      if (memberInfo != null)
 
369
        return GetAttribute<T>(memberInfo);
 
370
 
 
371
      return ReflectionUtils.GetAttribute<T>(attributeProvider, true);
 
372
    }
 
373
 
 
374
    private static bool? _dynamicCodeGeneration;
 
375
    private static bool? _fullyTrusted;
 
376
 
 
377
#if DEBUG
 
378
    internal static void SetFullyTrusted(bool fullyTrusted)
 
379
    {
 
380
      _fullyTrusted = fullyTrusted;
 
381
    }
 
382
 
 
383
    internal static void SetDynamicCodeGeneration(bool dynamicCodeGeneration)
 
384
    {
 
385
      _dynamicCodeGeneration = dynamicCodeGeneration;
 
386
    }
 
387
#endif
 
388
 
 
389
    public static bool DynamicCodeGeneration
 
390
    {
 
391
      get
 
392
      {
 
393
        if (_dynamicCodeGeneration == null)
 
394
        {
 
395
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
 
396
          try
 
397
          {
 
398
            new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand();
 
399
            new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess).Demand();
 
400
            new SecurityPermission(SecurityPermissionFlag.SkipVerification).Demand();
 
401
            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
 
402
            new SecurityPermission(PermissionState.Unrestricted).Demand();
 
403
            _dynamicCodeGeneration = true;
 
404
          }
 
405
          catch (Exception)
 
406
          {
 
407
            _dynamicCodeGeneration = false;
 
408
          }
 
409
#else
 
410
          _dynamicCodeGeneration = false;
 
411
#endif
 
412
        }
 
413
 
 
414
        return _dynamicCodeGeneration.Value;
 
415
      }
 
416
    }
 
417
 
 
418
    public static bool FullyTrusted
 
419
    {
 
420
      get
 
421
      {
 
422
        if (_fullyTrusted == null)
 
423
        {
 
424
#if (NETFX_CORE || SILVERLIGHT || PORTABLE)
 
425
          _fullyTrusted = false;
 
426
#elif !(NET20 || NET35)
 
427
          AppDomain appDomain = AppDomain.CurrentDomain;
 
428
 
 
429
          _fullyTrusted = appDomain.IsHomogenous && appDomain.IsFullyTrusted;
 
430
#else
 
431
          try
 
432
          {
 
433
            new SecurityPermission(PermissionState.Unrestricted).Demand();
 
434
            _fullyTrusted = true;
 
435
          }
 
436
          catch (Exception)
 
437
          {
 
438
            _fullyTrusted = false;
 
439
          }
 
440
#endif
 
441
        }
 
442
 
 
443
        return _fullyTrusted.Value;
 
444
      }
 
445
    }
 
446
 
 
447
    public static ReflectionDelegateFactory ReflectionDelegateFactory
 
448
    {
 
449
      get
 
450
      {
 
451
#if !(SILVERLIGHT || PORTABLE || NETFX_CORE)
 
452
        if (DynamicCodeGeneration)
 
453
          return DynamicReflectionDelegateFactory.Instance;
 
454
#endif
 
455
 
 
456
        return LateBoundReflectionDelegateFactory.Instance;
 
457
      }
 
458
    }
 
459
  }
300
460
}
 
 
b'\\ No newline at end of file'