diff options
author | Matthew Leibowitz <mattleibow@live.com> | 2016-02-22 17:45:28 +0300 |
---|---|---|
committer | Matthew Leibowitz <mattleibow@live.com> | 2016-02-22 17:45:28 +0300 |
commit | d95ccb2ca534a2126d58f3af3570e2e0df368f53 (patch) | |
tree | 16b92ab4903ef6ad25074ed449301753cd2c5c4f /mcs/class/Mono.Options | |
parent | f558ee9034335b252395a87ef87b8b39cc81e2df (diff) |
[Mono.Options] Changed the code a bit so that it works with PCL
- TypeDescriptor is unavailable, but basic conversion can be done using IConvertible
Diffstat (limited to 'mcs/class/Mono.Options')
-rw-r--r-- | mcs/class/Mono.Options/Mono.Options/Options.cs | 56 |
1 files changed, 46 insertions, 10 deletions
diff --git a/mcs/class/Mono.Options/Mono.Options/Options.cs b/mcs/class/Mono.Options/Mono.Options/Options.cs index 0519d9ed412..23ee99324e8 100644 --- a/mcs/class/Mono.Options/Mono.Options/Options.cs +++ b/mcs/class/Mono.Options/Mono.Options/Options.cs @@ -135,7 +135,11 @@ using System.ComponentModel; using System.Globalization; using System.IO; using System.Runtime.Serialization; +#if PCL +using System.Reflection; +#else using System.Security.Permissions; +#endif using System.Text; using System.Text.RegularExpressions; @@ -147,6 +151,12 @@ using System.Linq; using NDesk.Options; #endif +#if PCL +using MessageLocalizerConverter = System.Func<string, string>; +#else +using MessageLocalizerConverter = System.Converter<string, string>; +#endif + #if NDESK_OPTIONS namespace NDesk.Options #else @@ -448,15 +458,31 @@ namespace Mono.Options protected static T Parse<T> (string value, OptionContext c) { Type tt = typeof (T); - bool nullable = tt.IsValueType && tt.IsGenericType && - !tt.IsGenericTypeDefinition && - tt.GetGenericTypeDefinition () == typeof (Nullable<>); - Type targetType = nullable ? tt.GetGenericArguments () [0] : typeof (T); - TypeConverter conv = TypeDescriptor.GetConverter (targetType); +#if PCL + TypeInfo ti = tt.GetTypeInfo (); +#else + Type ti = tt; +#endif + bool nullable = + ti.IsValueType && + ti.IsGenericType && + !ti.IsGenericTypeDefinition && + ti.GetGenericTypeDefinition () == typeof (Nullable<>); +#if PCL + Type targetType = nullable ? tt.GenericTypeArguments [0] : tt; +#else + Type targetType = nullable ? tt.GetGenericArguments () [0] : tt; +#endif T t = default (T); try { - if (value != null) + if (value != null) { +#if PCL + t = (T) Convert.ChangeType (value, targetType); +#else + TypeConverter conv = TypeDescriptor.GetConverter (targetType); t = (T) conv.ConvertFromString (value); +#endif + } } catch (Exception e) { throw new OptionException ( @@ -572,10 +598,12 @@ namespace Mono.Options public abstract string Description { get; } public abstract bool GetArguments (string value, out IEnumerable<string> replacement); +#if !PCL public static IEnumerable<string> GetArgumentsFromFile (string file) { return GetArguments (File.OpenText (file), true); } +#endif public static IEnumerable<string> GetArguments (TextReader reader) { @@ -621,11 +649,12 @@ namespace Mono.Options } finally { if (close) - reader.Close (); + reader.Dispose (); } } } +#if !PCL public class ResponseFileSource : ArgumentSource { public override string[] GetNames () @@ -647,8 +676,11 @@ namespace Mono.Options return true; } } +#endif +#if !PCL [Serializable] +#endif public class OptionException : Exception { private string option; @@ -668,16 +700,19 @@ namespace Mono.Options this.option = optionName; } +#if !PCL protected OptionException (SerializationInfo info, StreamingContext context) : base (info, context) { this.option = info.GetString ("OptionName"); } +#endif public string OptionName { get {return this.option;} } +#if !PCL #pragma warning disable 618 // SecurityPermissionAttribute is obsolete [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)] #pragma warning restore 618 @@ -686,6 +721,7 @@ namespace Mono.Options base.GetObjectData (info, context); info.AddValue ("OptionName", option); } +#endif } public delegate void OptionAction<TKey, TValue> (TKey key, TValue value); @@ -697,15 +733,15 @@ namespace Mono.Options { } - public OptionSet (Converter<string, string> localizer) + public OptionSet (MessageLocalizerConverter localizer) { this.localizer = localizer; this.roSources = new ReadOnlyCollection<ArgumentSource>(sources); } - Converter<string, string> localizer; + MessageLocalizerConverter localizer; - public Converter<string, string> MessageLocalizer { + public MessageLocalizerConverter MessageLocalizer { get {return localizer;} } |