From 7a9aea0b52edd78a936bbb02a06917be3ec28294 Mon Sep 17 00:00:00 2001 From: Curtis Wensley Date: Sat, 6 Aug 2022 09:56:45 -0700 Subject: [PATCH] Fix detecting platform for macOS - Remove unused .NET Standard 1.0 code - Move PlatformDetect back into Eto.dll so it doesn't have to rely on platform-specific assemblies to detect the unix type --- src/Eto.Gtk/Eto.Gtk.csproj | 3 - src/Eto.Gtk/Eto.Gtk2.csproj | 3 - src/Eto.Gtk/Eto.Gtk3.csproj | 3 - src/Eto.Mac/Eto.Mac64.csproj | 3 - src/Eto.Mac/Eto.XamMac2.csproj | 3 - src/Eto.Mac/Eto.macOS.csproj | 3 - src/Eto/DisplayAttributeExtensions.cs | 81 ------------------- src/Eto/Drawing/Bitmap.cs | 4 - src/Eto/Drawing/Icon.cs | 4 - src/Eto/Drawing/IconFrame.cs | 4 - src/Eto/Forms/AboutDialog.cs | 4 - src/Eto/Forms/Binding/PropertyBinding.cs | 27 ------- src/Eto/Forms/Cursor.cs | 4 - .../ThemedPropertyGridHandler.cs | 3 - src/Eto/OperatingSystemPlatform.cs | 41 +++++----- src/Eto/PropertyDescriptorHelpers.cs | 74 +---------------- src/Eto/ReflectionBinaryFormatter.cs | 46 ----------- src/Eto/TypeHelper.cs | 76 ----------------- src/Shared/PlatformDetect.cs | 55 ------------- 19 files changed, 19 insertions(+), 422 deletions(-) delete mode 100644 src/Eto/ReflectionBinaryFormatter.cs delete mode 100644 src/Shared/PlatformDetect.cs diff --git a/src/Eto.Gtk/Eto.Gtk.csproj b/src/Eto.Gtk/Eto.Gtk.csproj index 55bc4cef46..314f4e0ab8 100755 --- a/src/Eto.Gtk/Eto.Gtk.csproj +++ b/src/Eto.Gtk/Eto.Gtk.csproj @@ -35,9 +35,6 @@ When building for the full framework, .NET Framework 4.6.1 or mono framework 5.1 Drawing\SplineHelper.cs - - PlatformDetect.cs - Forms\FixedMaskedTextProviderHandler.cs diff --git a/src/Eto.Gtk/Eto.Gtk2.csproj b/src/Eto.Gtk/Eto.Gtk2.csproj index dfbe745ea4..d1d7350b97 100644 --- a/src/Eto.Gtk/Eto.Gtk2.csproj +++ b/src/Eto.Gtk/Eto.Gtk2.csproj @@ -60,9 +60,6 @@ On Linux, mono-complete 5.10 or higher and gtk-sharp2 packages are required. Drawing\SplineHelper.cs - - PlatformDetect.cs - Forms\FixedMaskedTextProviderHandler.cs diff --git a/src/Eto.Gtk/Eto.Gtk3.csproj b/src/Eto.Gtk/Eto.Gtk3.csproj index ba5ba800c9..1b61f88cc6 100755 --- a/src/Eto.Gtk/Eto.Gtk3.csproj +++ b/src/Eto.Gtk/Eto.Gtk3.csproj @@ -73,9 +73,6 @@ On Linux, mono framework 5.10 or higher and gtk-sharp3 are required. Drawing\SplineHelper.cs - - PlatformDetect.cs - Forms\FixedMaskedTextProviderHandler.cs diff --git a/src/Eto.Mac/Eto.Mac64.csproj b/src/Eto.Mac/Eto.Mac64.csproj index b75f350696..6d2d4b96d1 100644 --- a/src/Eto.Mac/Eto.Mac64.csproj +++ b/src/Eto.Mac/Eto.Mac64.csproj @@ -48,9 +48,6 @@ You do not need to use any of the classes of this assembly (unless customizing t - - PlatformDetect.cs - Forms\FixedMaskedTextProviderHandler.cs diff --git a/src/Eto.Mac/Eto.XamMac2.csproj b/src/Eto.Mac/Eto.XamMac2.csproj index 6fc2e8deff..073a2a99fa 100644 --- a/src/Eto.Mac/Eto.XamMac2.csproj +++ b/src/Eto.Mac/Eto.XamMac2.csproj @@ -44,9 +44,6 @@ You do not need to use any of the classes of this assembly (unless customizing t - - PlatformDetect.cs - Forms\FixedMaskedTextProviderHandler.cs diff --git a/src/Eto.Mac/Eto.macOS.csproj b/src/Eto.Mac/Eto.macOS.csproj index 71d75a9012..d928b3adce 100644 --- a/src/Eto.Mac/Eto.macOS.csproj +++ b/src/Eto.Mac/Eto.macOS.csproj @@ -40,9 +40,6 @@ You do not need to use any of the classes of this assembly (unless customizing t - - PlatformDetect.cs - Forms\FixedMaskedTextProviderHandler.cs diff --git a/src/Eto/DisplayAttributeExtensions.cs b/src/Eto/DisplayAttributeExtensions.cs index f8f5d4924f..433e94488a 100644 --- a/src/Eto/DisplayAttributeExtensions.cs +++ b/src/Eto/DisplayAttributeExtensions.cs @@ -1,89 +1,11 @@ using System; using System.Reflection; using System.Linq; -#if NETSTANDARD2_0 using System.ComponentModel; using System.ComponentModel.DataAnnotations; -#endif namespace Eto { -#if NETSTANDARD1_0 - class BaseAttributeWrapper - where T: BaseAttributeWrapper.AttributeWrapper, new() - { - public abstract class AttributeWrapper - { - public object Instance { get; internal set; } - } - - protected static T Get(Type attributeType, Type type) - { - if (attributeType == null) - return null; - var instance = type.GetTypeInfo().GetCustomAttribute(attributeType); - if (instance == null) - return null; - var attr = new T(); - attr.Instance = instance; - return attr; - } - - protected static T Get(Type attributeType, IPropertyDescriptor descriptor) - { - if (attributeType == null) - return null; - var instance = descriptor.GetCustomAttribute(attributeType); - if (instance == null) - return null; - var attr = new T(); - attr.Instance = instance; - return attr; - } - } - - class EditorAttributeWrapper : BaseAttributeWrapper - { - static Type s_EditorAttributeType = - Type.GetType("System.ComponentModel.EditorAttribute, System") - ?? Type.GetType("System.ComponentModel.EditorAttribute, System.ComponentModel.TypeConverter") - ?? Type.GetType("System.ComponentModel.EditorAttribute, netstandard"); - static PropertyInfo s_EditorBaseTypeNameProperty = s_EditorAttributeType?.GetRuntimeProperty("EditorBaseTypeName"); - static PropertyInfo s_EditorTypeNameProperty = s_EditorAttributeType?.GetRuntimeProperty("EditorTypeName"); - - public class EditorAttribute : AttributeWrapper - { - public string EditorBaseTypeName => s_EditorBaseTypeNameProperty?.GetValue(Instance) as string; - public string EditorTypeName => s_EditorTypeNameProperty?.GetValue(Instance) as string; - } - - public static EditorAttribute Get(Type type) => Get(s_EditorAttributeType, type); - - public static EditorAttribute Get(IPropertyDescriptor descriptor) => Get(s_EditorAttributeType, descriptor); - } - - class DisplayAttributeWrapper : BaseAttributeWrapper - { - static Type s_DisplayAttributeType = - Type.GetType("System.ComponentModel.DataAnnotations.DisplayAttribute, System.ComponentModel.DataAnnotations") - ?? Type.GetType("System.ComponentModel.DataAnnotations.DisplayAttribute, System.ComponentModel.Annotations"); - static MethodInfo getNameMethod = s_DisplayAttributeType?.GetRuntimeMethod("GetName", new Type[0]); - static MethodInfo getGroupNameMethod = s_DisplayAttributeType?.GetRuntimeMethod("GetGroupName", new Type[0]); - static MethodInfo getDescriptionMethod = s_DisplayAttributeType?.GetRuntimeMethod("GetDescription", new Type[0]); - - public class DisplayAttribute : AttributeWrapper - { - public string GetName() => getNameMethod?.Invoke(Instance, null) as string; - public string GetGroupName() => getGroupNameMethod?.Invoke(Instance, null) as string; - public string GetDescription() => getDescriptionMethod?.Invoke(Instance, null) as string; - } - - public static DisplayAttribute Get(Type type) => Get(s_DisplayAttributeType, type); - - public static DisplayAttribute Get(IPropertyDescriptor descriptor) => Get(s_DisplayAttributeType, descriptor); - } - -#elif NETSTANDARD2_0 class BaseAttributeWrapper where T : Attribute { @@ -96,7 +18,4 @@ class BaseAttributeWrapper class EditorAttributeWrapper : BaseAttributeWrapper { } class DisplayAttributeWrapper : BaseAttributeWrapper { } -#else - Not Implemented. -#endif } diff --git a/src/Eto/Drawing/Bitmap.cs b/src/Eto/Drawing/Bitmap.cs index ae62b5a0b7..f21efbbf19 100644 --- a/src/Eto/Drawing/Bitmap.cs +++ b/src/Eto/Drawing/Bitmap.cs @@ -96,11 +96,7 @@ public static Bitmap FromResource(string resourceName, Assembly assembly = null) if (assembly == null) { -#if NETSTANDARD1_0 - assembly = (Assembly)TypeHelper.GetCallingAssembly.Invoke(null, null); -#else assembly = Assembly.GetCallingAssembly(); -#endif } using (var stream = assembly.GetManifestResourceStream(resourceName)) diff --git a/src/Eto/Drawing/Icon.cs b/src/Eto/Drawing/Icon.cs index cefe8596f2..789e14c569 100644 --- a/src/Eto/Drawing/Icon.cs +++ b/src/Eto/Drawing/Icon.cs @@ -108,11 +108,7 @@ public static Icon FromResource(string resourceName, Assembly assembly = null) { if (assembly == null) { - #if NETSTANDARD1_0 - assembly = (Assembly)TypeHelper.GetCallingAssembly.Invoke(null, null); - #else assembly = Assembly.GetCallingAssembly(); - #endif } if (resourceName.EndsWith(".ico", StringComparison.OrdinalIgnoreCase)) diff --git a/src/Eto/Drawing/IconFrame.cs b/src/Eto/Drawing/IconFrame.cs index de4d22caff..c5c18f986b 100644 --- a/src/Eto/Drawing/IconFrame.cs +++ b/src/Eto/Drawing/IconFrame.cs @@ -135,11 +135,7 @@ public static IconFrame FromResource(float scale, string resourceName, Assembly { if (assembly == null) { - #if NETSTANDARD1_0 - assembly = (Assembly)TypeHelper.GetCallingAssembly.Invoke(null, null); - #else assembly = Assembly.GetCallingAssembly(); - #endif } return new IconFrame(scale, () => diff --git a/src/Eto/Forms/AboutDialog.cs b/src/Eto/Forms/AboutDialog.cs index 01944ae8c7..65c68bb6b1 100644 --- a/src/Eto/Forms/AboutDialog.cs +++ b/src/Eto/Forms/AboutDialog.cs @@ -18,11 +18,7 @@ public class AboutDialog : CommonDialog /// [MethodImpl(MethodImplOptions.NoInlining)] public AboutDialog() -#if NETSTANDARD1_0 - : this(TypeHelper.GetCallingAssembly?.Invoke(null, null) as Assembly) -#else : this(Assembly.GetCallingAssembly()) -#endif { } diff --git a/src/Eto/Forms/Binding/PropertyBinding.cs b/src/Eto/Forms/Binding/PropertyBinding.cs index 20d40e42ae..53e5291549 100644 --- a/src/Eto/Forms/Binding/PropertyBinding.cs +++ b/src/Eto/Forms/Binding/PropertyBinding.cs @@ -22,9 +22,7 @@ namespace Eto.Forms public class PropertyBinding : IndirectBinding { Type declaringType; -#if !NETSTANDARD1_0 PropertyDescriptor descriptor; -#endif PropertyInfo propInfo; string property; @@ -64,7 +62,6 @@ public PropertyBinding(string property, bool ignoreCase = true) this.IgnoreCase = ignoreCase; } -#if !NETSTANDARD1_0 bool IsValid => descriptor != null || propInfo != null; bool CanRead => descriptor != null || propInfo?.CanRead == true; bool CanWrite => descriptor?.IsReadOnly == false || propInfo?.CanWrite == true; @@ -74,16 +71,6 @@ void Reset() propInfo = null; declaringType = null; } -#else - bool IsValid => propInfo != null; - bool CanRead => propInfo?.CanRead == true; - bool CanWrite => propInfo?.CanWrite == true; - void Reset() - { - propInfo = null; - declaringType = null; - } -#endif bool EnsureProperty(object dataItem) { @@ -102,7 +89,6 @@ bool EnsureProperty(object dataItem) return false; -#if !NETSTANDARD1_0 // use property descriptors first to support more scenarios descriptor = sc.TypeDescriptor.GetProperties(dataItem).Find(Property, IgnoreCase); if (descriptor != null) @@ -111,7 +97,6 @@ bool EnsureProperty(object dataItem) propInfo = null; return true; } -#endif // iterate to find non-public properties or with different case var comparison = IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; @@ -151,11 +136,7 @@ protected override T InternalGetValue(object dataItem) if (EnsureProperty(dataItem) && CanRead) { var propertyType = typeof(T); -#if !NETSTANDARD1_0 object val = descriptor != null ? descriptor.GetValue(dataItem) : propInfo.GetValue(dataItem); -#else - object val = propInfo.GetValue(dataItem); -#endif if (val != null && !propertyType.IsInstanceOfType(val)) { try @@ -183,11 +164,7 @@ protected override void InternalSetValue(object dataItem, T value) { if (EnsureProperty(dataItem) && CanWrite) { -#if !NETSTANDARD1_0 var propertyType = descriptor?.PropertyType ?? propInfo.PropertyType; -#else - var propertyType = propInfo.PropertyType; -#endif object val = value; if (val != null && !propertyType.IsInstanceOfType(val)) { @@ -202,14 +179,10 @@ protected override void InternalSetValue(object dataItem, T value) val = propertyType.GetTypeInfo().IsValueType ? Activator.CreateInstance(propertyType) : null; } } -#if !NETSTANDARD1_0 if (descriptor != null) descriptor.SetValue(dataItem, val); else if (propInfo != null) propInfo.SetValue(dataItem, val); -#else - propInfo.SetValue(dataItem, val); -#endif } } diff --git a/src/Eto/Forms/Cursor.cs b/src/Eto/Forms/Cursor.cs index 1f3771d2a7..0944b3b615 100644 --- a/src/Eto/Forms/Cursor.cs +++ b/src/Eto/Forms/Cursor.cs @@ -152,11 +152,7 @@ public static Cursor FromResource(string resourceName, Assembly assembly = null) if (assembly == null) { -#if NETSTANDARD1_0 - assembly = (Assembly)TypeHelper.GetCallingAssembly.Invoke(null, null); -#else assembly = Assembly.GetCallingAssembly(); -#endif } using (var stream = assembly.GetManifestResourceStream(resourceName)) diff --git a/src/Eto/Forms/ThemedControls/ThemedPropertyGridHandler.cs b/src/Eto/Forms/ThemedControls/ThemedPropertyGridHandler.cs index 50d86f81ec..90da37af44 100644 --- a/src/Eto/Forms/ThemedControls/ThemedPropertyGridHandler.cs +++ b/src/Eto/Forms/ThemedControls/ThemedPropertyGridHandler.cs @@ -9,9 +9,6 @@ using System.Runtime.CompilerServices; using System.Collections.ObjectModel; using sc = System.ComponentModel; -#if !NETSTANDARD1_0 -using System.ComponentModel.DataAnnotations; -#endif namespace Eto.Forms.ThemedControls { diff --git a/src/Eto/OperatingSystemPlatform.cs b/src/Eto/OperatingSystemPlatform.cs index b40873b2e1..11ee8073cd 100644 --- a/src/Eto/OperatingSystemPlatform.cs +++ b/src/Eto/OperatingSystemPlatform.cs @@ -52,36 +52,31 @@ public sealed class OperatingSystemPlatform /// public bool IsLinux { get; private set; } - #if NETSTANDARD + [DllImport("libc")] + static extern int uname(IntPtr buf); static string GetUnixType() { - // need at least one of these platforms if we're detecting the unix type (mac/linux) in PCL - var detectType = Type.GetType("Eto.PlatformDetect, Eto.XamMac2", false) - ?? Type.GetType("Eto.PlatformDetect, Eto.Mac64", false) - ?? Type.GetType("Eto.PlatformDetect, Eto.XamMac", false) - ?? Type.GetType("Eto.PlatformDetect, Eto.Mac", false) - ?? Type.GetType("Eto.PlatformDetect, Eto.Gtk", false) - ?? Type.GetType("Eto.PlatformDetect, Eto.Gtk2", false) - ?? Type.GetType("Eto.PlatformDetect, Eto.Gtk3", false); - if (detectType != null) + IntPtr buf = IntPtr.Zero; + string osName = ""; + try { - var getUnixTypeMethod = detectType.GetRuntimeMethod("GetUnixType", new Type[] { }); - if (getUnixTypeMethod != null) - return (string)getUnixTypeMethod.Invoke(null, null); + buf = Marshal.AllocHGlobal(8192); + if (uname(buf) == 0) + osName = Marshal.PtrToStringAnsi(buf); } - return string.Empty; + // Analysis disable once EmptyGeneralCatchClause + catch + { + } + finally + { + if (buf != IntPtr.Zero) + Marshal.FreeHGlobal(buf); + } + return osName; } - - #else - static string GetUnixType() - { - return PlatformDetect.GetUnixType(); - } - - #endif - /// /// Initializes a new instance of the OperatingSystemPlatform class /// diff --git a/src/Eto/PropertyDescriptorHelpers.cs b/src/Eto/PropertyDescriptorHelpers.cs index 744fa6fe73..13540526e4 100644 --- a/src/Eto/PropertyDescriptorHelpers.cs +++ b/src/Eto/PropertyDescriptorHelpers.cs @@ -67,76 +67,6 @@ public Attribute GetCustomAttribute(Type attributeType) public void SetValue(object obj, object value) => _property.SetValue(obj, value); } -#if NETSTANDARD1_0 - class PropertyDescriptorDescriptor : IPropertyDescriptor - { - object _propertyDescriptor; - static Type s_PropertyDescriptorType = - Type.GetType("System.ComponentModel.PropertyDescriptor, System") - ?? Type.GetType("System.ComponentModel.PropertyDescriptor, System.ComponentModel.TypeConverter") - ?? Type.GetType("System.ComponentModel.PropertyDescriptor, netstandard"); - - static PropertyInfo s_ComponentTypeProperty = s_PropertyDescriptorType?.GetRuntimeProperty("ComponentType"); - static PropertyInfo s_PropertyTypeProperty = s_PropertyDescriptorType?.GetRuntimeProperty("PropertyType"); - static PropertyInfo s_NameProperty = s_PropertyDescriptorType?.GetRuntimeProperty("Name"); - static PropertyInfo s_DisplayNameProperty = s_PropertyDescriptorType?.GetRuntimeProperty("DisplayName"); - static PropertyInfo s_ReadOnlyProperty = s_PropertyDescriptorType?.GetRuntimeProperty("IsReadOnly"); - static PropertyInfo s_IsBrowsableProperty = s_PropertyDescriptorType?.GetRuntimeProperty("IsBrowsable"); - static PropertyInfo s_ConverterProperty = s_PropertyDescriptorType?.GetRuntimeProperty("Converter"); - static MethodInfo s_GetValueMethod = s_PropertyDescriptorType?.GetRuntimeMethod("GetValue", new Type[] { typeof(object) }); - static MethodInfo s_SetValueMethod = s_PropertyDescriptorType?.GetRuntimeMethod("SetValue", new Type[] { typeof(object), typeof(object) }); - static PropertyInfo s_AttributesProperty = s_PropertyDescriptorType?.GetRuntimeProperty("Attributes"); - - public static bool IsSupported => s_PropertyDescriptorType != null; - - public static PropertyDescriptorDescriptor Get(object obj) - { - if (!IsSupported) - return null; - return new PropertyDescriptorDescriptor(obj); - } - - public PropertyDescriptorDescriptor(object descriptor) - { - _propertyDescriptor = descriptor; - } - - public Type ComponentType => s_ComponentTypeProperty?.GetValue(_propertyDescriptor) as Type; - - public Type PropertyType => s_PropertyTypeProperty?.GetValue(_propertyDescriptor) as Type; - - public string Name => s_NameProperty?.GetValue(_propertyDescriptor) as string; - - public string DisplayName => s_DisplayNameProperty?.GetValue(_propertyDescriptor) as string; - - public bool IsReadOnly => Equals(s_ReadOnlyProperty?.GetValue(_propertyDescriptor), true); - - public bool CanRead => true; - - public bool IsBrowsable => Equals(s_IsBrowsableProperty?.GetValue(_propertyDescriptor), true); - - public sc.TypeConverter Converter => s_ConverterProperty?.GetValue(_propertyDescriptor) as sc.TypeConverter; - - public object GetValue(object obj) => s_GetValueMethod?.Invoke(_propertyDescriptor, new object[] { obj }); - - public void SetValue(object obj, object value) => s_SetValueMethod?.Invoke(_propertyDescriptor, new object[] { obj, value }); - - ICollection Attributes - { - get => s_AttributesProperty?.GetValue(_propertyDescriptor) as ICollection; - } - - public Attribute GetCustomAttribute(Type attributeType) - { - return Attributes.OfType().FirstOrDefault(r => r.GetType() == attributeType); - } - - public T GetCustomAttribute() where T : Attribute - { - return Attributes.OfType().FirstOrDefault(); - } - } -#elif NETSTANDARD2_0 class PropertyDescriptorDescriptor : IPropertyDescriptor { PropertyDescriptor _propertyDescriptor; @@ -188,9 +118,7 @@ public T GetCustomAttribute() where T : Attribute return Attributes.OfType().FirstOrDefault(); } } -#else - Not Implemented. -#endif + static class EtoTypeDescriptor { public static IPropertyDescriptor Get(object obj) diff --git a/src/Eto/ReflectionBinaryFormatter.cs b/src/Eto/ReflectionBinaryFormatter.cs deleted file mode 100644 index 96ecf4aea3..0000000000 --- a/src/Eto/ReflectionBinaryFormatter.cs +++ /dev/null @@ -1,46 +0,0 @@ -#if NETSTANDARD1_0 -using System; -using System.IO; -using System.Reflection; - -namespace System.Runtime.Serialization.Formatters.Binary -{ - // so we don't need #if's everywhere -} - -namespace Eto -{ - /// - /// BinaryFormatter via reflection used for .net standard 1.0 compatibility - /// - class BinaryFormatter - { - - static Type s_BinaryFormatterType = Type.GetType("System.Runtime.Serialization.Formatters.Binary.BinaryFormatter") - ?? Type.GetType("System.Runtime.Serialization.Formatters.Binary.BinaryFormatter, System.Runtime.Serialization.Formatters") - ?? Type.GetType("System.Runtime.Serialization.Formatters.Binary.BinaryFormatter, netstandard"); - static MethodInfo s_SerializeMethod = s_BinaryFormatterType?.GetRuntimeMethod("Serialize", new[] { typeof(Stream), typeof(object) }); - static MethodInfo s_DeserializeMethod = s_BinaryFormatterType?.GetRuntimeMethod("Deserialize", new[] { typeof(Stream) }); - - object binaryFormatter; - - public BinaryFormatter() - { - if (s_BinaryFormatterType == null || s_SerializeMethod == null || s_DeserializeMethod == null) - throw new InvalidOperationException("Could not create an instance of BinaryFormatter"); - - binaryFormatter = Activator.CreateInstance(s_BinaryFormatterType); - } - - public void Serialize(Stream stream, object value) - { - s_SerializeMethod.Invoke(binaryFormatter, new object[] { stream, value }); - } - - public object Deserialize(Stream stream) - { - return s_DeserializeMethod.Invoke(binaryFormatter, new object[] { stream }); - } - } -} -#endif diff --git a/src/Eto/TypeHelper.cs b/src/Eto/TypeHelper.cs index 9905c6e354..a8f1db62c6 100644 --- a/src/Eto/TypeHelper.cs +++ b/src/Eto/TypeHelper.cs @@ -13,105 +13,30 @@ namespace Eto /// static class TypeHelper { -#if NETSTANDARD1_0 - static MethodInfo getCallingAssembly = typeof(Assembly).GetTypeInfo().GetDeclaredMethod("GetCallingAssembly"); - - static TypeHelper() - { - var detectType = Type.GetType("Eto.PlatformDetect, Eto.Gtk", false); - if (detectType != null) - getCallingAssembly = detectType.GetRuntimeMethod("GetCallingAssembly", new Type[] { }); - } - - public static MethodInfo GetCallingAssembly => getCallingAssembly ?? throw new ArgumentNullException("assembly", "This platform doesn't support Assembly.GetCallingAssembly(), so you must pass the assembly directly"); -#endif - public static Assembly GetAssembly(this Type type) { -#if NETSTANDARD1_0 - return type.GetTypeInfo().Assembly; -#else return type.Assembly; -#endif } public static Type GetBaseType(this Type type) { -#if NETSTANDARD1_0 - return type.GetTypeInfo().BaseType; -#else return type.BaseType; -#endif } public static bool IsEnum(this Type type) { -#if NETSTANDARD1_0 - return type.GetTypeInfo().IsEnum; -#else return type.IsEnum; -#endif } public static MethodInfo GetGetMethod(this PropertyInfo propertyInfo) { -#if NETSTANDARD1_0 - return propertyInfo.GetMethod; -#else return propertyInfo.GetGetMethod(true); -#endif } public static MethodInfo GetSetMethod(this PropertyInfo propertyInfo) { -#if NETSTANDARD1_0 - return propertyInfo.SetMethod; -#else return propertyInfo.GetSetMethod(true); -#endif - } - -#if NETSTANDARD1_0 - public static T GetCustomAttribute(this Type type, bool inherit) - where T: Attribute - { - return CustomAttributeExtensions.GetCustomAttribute(type.GetTypeInfo(), inherit); - } - - public static MethodInfo GetAddMethod(this EventInfo eventInfo) - { - return eventInfo.AddMethod; - } - - public static MethodInfo GetRemoveMethod(this EventInfo eventInfo) - { - return eventInfo.RemoveMethod; - } - /// - /// Determines whether the specified object is an instance of the current Type. - /// - /// The type. - /// The object to compare with the current type. - /// true if the current Type is in the inheritance hierarchy of the - /// object represented by o, or if the current Type is an interface that o - /// supports. false if neither of these conditions is the case, or if o is - /// null, or if the current Type is an open generic type (that is, - /// ContainsGenericParameters returns true). - public static bool IsInstanceOfType(this Type type, object o) - { - return o != null && IsAssignableFrom(type, o.GetType()); - } - - public static bool IsAssignableFrom(this Type type, Type c) - { - return c != null && type.GetTypeInfo().IsAssignableFrom(c.GetTypeInfo()); - } - - public static ConstructorInfo GetConstructor(this Type type, Type[] args) - { - return type.GetTypeInfo().DeclaredConstructors.FirstOrDefault(r => r.GetParameters().Select(p => p.ParameterType).SequenceEqual(args)); } -#else public static T GetCustomAttribute(this Type type, bool inherit) where T : Attribute @@ -168,6 +93,5 @@ public static IEnumerable GetRuntimeFields(this Type type) { return type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); } -#endif } } diff --git a/src/Shared/PlatformDetect.cs b/src/Shared/PlatformDetect.cs deleted file mode 100644 index e85f6d28ce..0000000000 --- a/src/Shared/PlatformDetect.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using System.Diagnostics; -using System.Reflection; -using System.Runtime.InteropServices; - -namespace Eto -{ -#if OSX - [Preserve(AllMembers = true)] -#endif - static class PlatformDetect - { - [DllImport("libc")] - static extern int uname(IntPtr buf); - - public static string GetUnixType() - { - IntPtr buf = IntPtr.Zero; - string osName = ""; - try - { - buf = Marshal.AllocHGlobal(8192); - if (uname(buf) == 0) - osName = Marshal.PtrToStringAnsi(buf); - } - // Analysis disable once EmptyGeneralCatchClause - catch - { - } - finally - { - if (buf != IntPtr.Zero) - Marshal.FreeHGlobal(buf); - } - return osName; - } - -#if GTK3 - private static Assembly _etoassembly = typeof(Eto.Forms.Button).Assembly; - - public static Assembly GetCallingAssembly() - { - var s = new StackTrace(); - - for (int i = 0; i < s.FrameCount; i++) - { - if (_etoassembly.Equals(s.GetFrame(i).GetMethod().DeclaringType.Assembly)) - return s.GetFrame(i + 1).GetMethod().DeclaringType.Assembly; - } - - throw new Exception("Failed to get executing assembly."); - } -#endif - } -}