From 1540749175796f1763ba47d1a56e79007cd4a3bf Mon Sep 17 00:00:00 2001 From: Frederik Carlier Date: Fri, 17 Mar 2017 23:30:35 +0100 Subject: [PATCH] Make System.Drawing code compatible with .NET Core (#4548) --- .../InvalidPrinterException.cs | 2 +- mcs/class/System.Drawing/System.Drawing/Bitmap.cs | 7 ++++++- mcs/class/System.Drawing/System.Drawing/Font.cs | 8 +++----- mcs/class/System.Drawing/System.Drawing/Icon.cs | 13 +++++++++---- mcs/class/System.Drawing/System.Drawing/Image.cs | 2 +- .../system/componentmodel/EditorAttribute.cs | 4 ++-- 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/mcs/class/System.Drawing/System.Drawing.Printing/InvalidPrinterException.cs b/mcs/class/System.Drawing/System.Drawing.Printing/InvalidPrinterException.cs index 02a3f27c9a5..22573086370 100644 --- a/mcs/class/System.Drawing/System.Drawing.Printing/InvalidPrinterException.cs +++ b/mcs/class/System.Drawing/System.Drawing.Printing/InvalidPrinterException.cs @@ -56,7 +56,7 @@ namespace System.Drawing.Printing { base.GetObjectData (info, context); } - + private static string GetMessage(PrinterSettings settings) { if (settings.PrinterName == null || settings.PrinterName == String.Empty) diff --git a/mcs/class/System.Drawing/System.Drawing/Bitmap.cs b/mcs/class/System.Drawing/System.Drawing/Bitmap.cs index ffa825c5f27..7a7d8c68952 100644 --- a/mcs/class/System.Drawing/System.Drawing/Bitmap.cs +++ b/mcs/class/System.Drawing/System.Drawing/Bitmap.cs @@ -37,6 +37,7 @@ using System.IO; using System.Drawing.Imaging; +using System.Reflection; using System.Runtime.Serialization; using System.Runtime.InteropServices; using System.ComponentModel; @@ -132,7 +133,11 @@ namespace System.Drawing if (resource == null) throw new ArgumentException ("resource"); - Stream s = type.Assembly.GetManifestResourceStream (type, resource); + // For compatibility with the .NET Framework + if (type == null) + throw new NullReferenceException(); + + Stream s = type.GetTypeInfo ().Assembly.GetManifestResourceStream (type, resource); if (s == null) { string msg = Locale.GetText ("Resource '{0}' was not found.", resource); throw new FileNotFoundException (msg); diff --git a/mcs/class/System.Drawing/System.Drawing/Font.cs b/mcs/class/System.Drawing/System.Drawing/Font.cs index 34117a2e193..31861d51238 100644 --- a/mcs/class/System.Drawing/System.Drawing/Font.cs +++ b/mcs/class/System.Drawing/System.Drawing/Font.cs @@ -31,6 +31,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // +using System.Reflection; using System.Runtime.Serialization; using System.Runtime.InteropServices; using System.ComponentModel; @@ -427,10 +428,7 @@ namespace System.Drawing [Browsable(false)] public bool IsSystemFont { get { - if (systemFontName == null) - return false; - - return StringComparer.InvariantCulture.Compare (systemFontName, string.Empty) != 0; + return !string.IsNullOrEmpty (systemFontName); } } @@ -624,7 +622,7 @@ namespace System.Drawing } Type st = logFont.GetType (); - if (!st.IsLayoutSequential) + if (!st.GetTypeInfo ().IsLayoutSequential) throw new ArgumentException ("logFont", Locale.GetText ("Layout must be sequential.")); // note: there is no exception if 'logFont' isn't big enough diff --git a/mcs/class/System.Drawing/System.Drawing/Icon.cs b/mcs/class/System.Drawing/System.Drawing/Icon.cs index 8352f15c670..6fe88d5a2e0 100644 --- a/mcs/class/System.Drawing/System.Drawing/Icon.cs +++ b/mcs/class/System.Drawing/System.Drawing/Icon.cs @@ -36,6 +36,7 @@ using System.Collections; using System.ComponentModel; using System.Drawing.Imaging; using System.IO; +using System.Reflection; using System.Runtime.Serialization; using System.Runtime.InteropServices; @@ -229,7 +230,11 @@ namespace System.Drawing if (resource == null) throw new ArgumentException ("resource"); - using (Stream s = type.Assembly.GetManifestResourceStream (type, resource)) { + // For compatibility with the .NET Framework + if (type == null) + throw new NullReferenceException(); + + using (Stream s = type.GetTypeInfo ().Assembly.GetManifestResourceStream (type, resource)) { if (s == null) { string msg = Locale.GetText ("Resource '{0}' was not found.", resource); throw new FileNotFoundException (msg); @@ -261,7 +266,7 @@ namespace System.Drawing internal Icon (string resourceName, bool undisposable) { - using (Stream s = typeof (Icon).Assembly.GetManifestResourceStream (resourceName)) { + using (Stream s = typeof (Icon).GetTypeInfo ().Assembly.GetManifestResourceStream (resourceName)) { if (s == null) { string msg = Locale.GetText ("Resource '{0}' was not found.", resourceName); throw new FileNotFoundException (msg); @@ -859,10 +864,10 @@ Console.WriteLine ("\tbih.biClrImportant: {0}", bih.biClrImportant); } imageData [j] = iidata; - bihReader.Close(); + bihReader.Dispose (); } - reader.Close(); + reader.Dispose (); } } } diff --git a/mcs/class/System.Drawing/System.Drawing/Image.cs b/mcs/class/System.Drawing/System.Drawing/Image.cs index c5af3c18e1b..6bd89918dd6 100644 --- a/mcs/class/System.Drawing/System.Drawing/Image.cs +++ b/mcs/class/System.Drawing/System.Drawing/Image.cs @@ -794,7 +794,7 @@ public abstract class Image : MarshalByRefObject, IDisposable , ICloneable, ISer Status status = GDIPlus.GdipDisposeImage (nativeObject); // dispose the stream (set under Win32 only if SD owns the stream) and ... if (stream != null) { - stream.Close (); + stream.Dispose (); stream = null; } // ... set nativeObject to null before (possibly) throwing an exception diff --git a/mcs/class/referencesource/System/compmod/system/componentmodel/EditorAttribute.cs b/mcs/class/referencesource/System/compmod/system/componentmodel/EditorAttribute.cs index c3f493f88dc..436cc7b930e 100644 --- a/mcs/class/referencesource/System/compmod/system/componentmodel/EditorAttribute.cs +++ b/mcs/class/referencesource/System/compmod/system/componentmodel/EditorAttribute.cs @@ -35,7 +35,7 @@ namespace System.ComponentModel { /// name of the editor. /// public EditorAttribute(string typeName, string baseTypeName) { - string temp = typeName.ToUpper(CultureInfo.InvariantCulture); + string temp = typeName.ToUpperInvariant (); Debug.Assert(temp.IndexOf(".DLL") == -1, "Came across: " + typeName + " . Please remove the .dll extension"); this.typeName = typeName; this.baseTypeName = baseTypeName; @@ -45,7 +45,7 @@ namespace System.ComponentModel { /// Initializes a new instance of the class. /// public EditorAttribute(string typeName, Type baseType) { - string temp = typeName.ToUpper(CultureInfo.InvariantCulture); + string temp = typeName.ToUpperInvariant (); Debug.Assert(temp.IndexOf(".DLL") == -1, "Came across: " + typeName + " . Please remove the .dll extension"); this.typeName = typeName; this.baseTypeName = baseType.AssemblyQualifiedName; -- 2.25.1