diff --git a/src/Mono.Android/Android.Graphics/Color.cs b/src/Mono.Android/Android.Graphics/Color.cs index bf57968663c..97a46fd5b11 100644 --- a/src/Mono.Android/Android.Graphics/Color.cs +++ b/src/Mono.Android/Android.Graphics/Color.cs @@ -395,11 +395,18 @@ public static void RGBToHSV (int red, int green, int blue, float[] hsv) public class ColorValueMarshaler : JniValueMarshaler { + const DynamicallyAccessedMemberTypes ConstructorsAndInterfaces = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.Interfaces; + const string ExpressionRequiresUnreferencedCode = "System.Linq.Expression usage may trim away required code."; + public override Type MarshalType { get { return typeof (int); } } - public override Color CreateGenericValue (ref JniObjectReference reference, JniObjectReferenceOptions options, Type targetType) + public override Color CreateGenericValue ( + ref JniObjectReference reference, + JniObjectReferenceOptions options, + [DynamicallyAccessedMembers (ConstructorsAndInterfaces)] + Type targetType) { throw new NotImplementedException (); } @@ -414,6 +421,7 @@ public override void DestroyGenericArgumentState (Color value, ref JniValueMarsh throw new NotImplementedException (); } + [RequiresUnreferencedCode (ExpressionRequiresUnreferencedCode)] public override Expression CreateParameterToManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize, Type targetType) { var c = typeof (Color).GetConstructor (new[]{typeof (int)})!; @@ -424,6 +432,7 @@ public override Expression CreateParameterToManagedExpression (JniValueMarshaler return v; } + [RequiresUnreferencedCode (ExpressionRequiresUnreferencedCode)] public override Expression CreateParameterFromManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize) { var r = Expression.Variable (MarshalType, sourceValue.Name + "_p"); @@ -433,6 +442,7 @@ public override Expression CreateParameterFromManagedExpression (JniValueMarshal return r; } + [RequiresUnreferencedCode (ExpressionRequiresUnreferencedCode)] public override Expression CreateReturnValueFromManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue) { return CreateParameterFromManagedExpression (context, sourceValue, 0); diff --git a/src/Mono.Android/Android.Runtime/IJavaObjectValueMarshaler.cs b/src/Mono.Android/Android.Runtime/IJavaObjectValueMarshaler.cs index f5cab61ec1d..89436ab2b08 100644 --- a/src/Mono.Android/Android.Runtime/IJavaObjectValueMarshaler.cs +++ b/src/Mono.Android/Android.Runtime/IJavaObjectValueMarshaler.cs @@ -10,9 +10,16 @@ namespace Android.Runtime { sealed class IJavaObjectValueMarshaler : JniValueMarshaler { + const DynamicallyAccessedMemberTypes ConstructorsAndInterfaces = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.Interfaces; + const string ExpressionRequiresUnreferencedCode = "System.Linq.Expression usage may trim away required code."; + internal static IJavaObjectValueMarshaler Instance = new IJavaObjectValueMarshaler (); - public override IJavaObject CreateGenericValue (ref JniObjectReference reference, JniObjectReferenceOptions options, Type? targetType) + public override IJavaObject CreateGenericValue ( + ref JniObjectReference reference, + JniObjectReferenceOptions options, + [DynamicallyAccessedMembers (ConstructorsAndInterfaces)] + Type? targetType) { throw new NotImplementedException (); } @@ -27,6 +34,7 @@ public override void DestroyGenericArgumentState ([AllowNull]IJavaObject value, throw new NotImplementedException (); } + [RequiresUnreferencedCode (ExpressionRequiresUnreferencedCode)] public override Expression CreateReturnValueFromManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue) { return Expression.Call ( @@ -36,6 +44,7 @@ public override Expression CreateReturnValueFromManagedExpression (JniValueMarsh sourceValue); } + [RequiresUnreferencedCode (ExpressionRequiresUnreferencedCode)] public override Expression CreateParameterToManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize, Type? targetType) { var r = Expression.Variable (targetType, sourceValue.Name + "_val"); diff --git a/src/Mono.Android/System.Drawing/PointConverter.cs b/src/Mono.Android/System.Drawing/PointConverter.cs index 0255e55c87e..0b06d1a2d09 100644 --- a/src/Mono.Android/System.Drawing/PointConverter.cs +++ b/src/Mono.Android/System.Drawing/PointConverter.cs @@ -30,6 +30,7 @@ using System.Collections; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.ComponentModel.Design.Serialization; using System.Runtime.CompilerServices; @@ -133,6 +134,7 @@ public override bool GetCreateInstanceSupported (ITypeDescriptorContext context) return true; } + [RequiresUnreferencedCode("The Type of value cannot be statically discovered.")] public override PropertyDescriptorCollection? GetProperties ( ITypeDescriptorContext context, object value, Attribute[] attributes) diff --git a/src/Mono.Android/System.Drawing/RectangleConverter.cs b/src/Mono.Android/System.Drawing/RectangleConverter.cs index 364784f495f..332f94d86ea 100644 --- a/src/Mono.Android/System.Drawing/RectangleConverter.cs +++ b/src/Mono.Android/System.Drawing/RectangleConverter.cs @@ -31,6 +31,7 @@ using System.ComponentModel; using System.Collections; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Text; using System.ComponentModel.Design.Serialization; @@ -147,6 +148,7 @@ public override bool GetCreateInstanceSupported (ITypeDescriptorContext context) return true; } + [RequiresUnreferencedCode("The Type of value cannot be statically discovered.")] public override PropertyDescriptorCollection? GetProperties ( ITypeDescriptorContext context, object value, Attribute[] attributes) diff --git a/src/Mono.Android/System.Drawing/SizeConverter.cs b/src/Mono.Android/System.Drawing/SizeConverter.cs index 53fe055733c..2cf096d2a56 100644 --- a/src/Mono.Android/System.Drawing/SizeConverter.cs +++ b/src/Mono.Android/System.Drawing/SizeConverter.cs @@ -31,6 +31,7 @@ using System.Collections; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.ComponentModel.Design.Serialization; using System.Reflection; @@ -135,6 +136,7 @@ public override bool GetCreateInstanceSupported (ITypeDescriptorContext context) return true; } + [RequiresUnreferencedCode("The Type of value cannot be statically discovered.")] public override PropertyDescriptorCollection? GetProperties ( ITypeDescriptorContext context, object value, Attribute[] attributes) diff --git a/src/Mono.Android/System.Drawing/SizeFConverter.cs b/src/Mono.Android/System.Drawing/SizeFConverter.cs index 8933839fba2..5d831e30c52 100644 --- a/src/Mono.Android/System.Drawing/SizeFConverter.cs +++ b/src/Mono.Android/System.Drawing/SizeFConverter.cs @@ -32,6 +32,7 @@ using System; using System.Collections; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.ComponentModel.Design.Serialization; using System.Reflection; @@ -118,6 +119,7 @@ public override bool GetCreateInstanceSupported (ITypeDescriptorContext context) return true; } + [RequiresUnreferencedCode("The Type of value cannot be statically discovered.")] public override PropertyDescriptorCollection? GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes) { if (value is SizeF) diff --git a/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs b/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs index 83e858e1a62..f12317c1ead 100644 --- a/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs +++ b/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs @@ -313,14 +313,9 @@ protected virtual Task SetupRequest (HttpRequestMessage request, HttpURLConnecti object? GetUnderlyingHandler () { var fieldName = "_nativeHandler"; - FieldInfo? field = null; - - for (var type = GetType (); type != null; type = type.BaseType) { - field = type.GetField (fieldName, BindingFlags.Instance | BindingFlags.NonPublic); - if (field != null) - break; - } - + const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; + FieldInfo? field = typeof (HttpClientHandler).GetField (fieldName, flags) ?? + typeof (HttpMessageHandler).GetField (fieldName, flags); if (field == null) { throw new InvalidOperationException ($"Field '{fieldName}' is missing from type '{GetType ()}'."); }