ImageResizer  3.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Static Public Member Functions | List of all members
ImageResizer.ExtensionMethods.NameValueCollectionExtensions Class Reference

Static Public Member Functions

static T Get< T > (NameValueCollection t, string name)
 Provides culture-invariant parsing of int, double, float, bool, and enum values. More...
 
static T Get< T > (NameValueCollection q, string name, T?defaultValue)
 Provides culture-invariant parsing of int, double, float, bool, and enum values. More...
 
static T Get< T > (NameValueCollection q, string name, T defaultValue)
 Provides culture-invariant parsing of int, double, float, bool, and enum values. More...
 
static T ParsePrimitive< T > (string value, T?defaultValue)
 Provides culture-invariant parsing of int, double, float, bool, and enum values. More...
 
static string SerializePrimitive< T > (T?val)
 
static NameValueCollection SetAsString< T > (NameValueCollection q, string name, T val)
 Serializes the given value by calling .ToString(). If the value is null, the key is removed. More...
 
static NameValueCollection Set< T > (NameValueCollection q, string name, T?val)
 Provides culture-invariant serialization of value types, in lower case for querystring readability. Setting a key to null removes it. More...
 
static T[] GetList< T > (NameValueCollection q, string name, T?fallbackValue, params int[] allowedSizes)
 
static T[] ParseList< T > (string text, T?fallbackValue, params int[] allowedSizes)
 Parses a comma-delimited list of primitive values. If there are unparsable items in the list, they will be replaced with 'fallbackValue'. If fallbackValue is null, the function will return null More...
 
static NameValueCollection SetList< T > (NameValueCollection q, string name, T[] values, bool throwExceptions, params int[] allowedSizes)
 
static bool IsOneSpecified (NameValueCollection q, params string[] keys)
 Returns true if any of the specified keys contain a value More...
 
static NameValueCollection Normalize (NameValueCollection q, string primary, string secondary)
 Normalizes a command that has two possible names. If either of the commands has a null or empty value, those keys are removed. If both the the primary and secondary are present, the secondary is removed. Otherwise, the secondary is renamed to the primary name. More...
 
static NameValueCollection Keep (NameValueCollection q, params string[] keysToKeep)
 Creates and returns a new NameValueCollection instance that contains only the specified keys from the current collection. More...
 

Detailed Description

Definition at line 10 of file NameValueCollection.cs.

Member Function Documentation

static T ImageResizer.ExtensionMethods.NameValueCollectionExtensions.Get< T > ( NameValueCollection  t,
string  name 
)
inlinestatic

Provides culture-invariant parsing of int, double, float, bool, and enum values.

Template Parameters
T
Parameters
t
name
Returns
Type Constraints
T :struct 
T :IConvertible 

Definition at line 22 of file NameValueCollection.cs.

22  : struct, IConvertible {
23  return Get<T>(t, name, null);
24  }
static T Get< T >(NameValueCollection t, string name)
Provides culture-invariant parsing of int, double, float, bool, and enum values.
static T ImageResizer.ExtensionMethods.NameValueCollectionExtensions.Get< T > ( NameValueCollection  q,
string  name,
T?  defaultValue 
)
inlinestatic

Provides culture-invariant parsing of int, double, float, bool, and enum values.

Template Parameters
T
Parameters
q
name
defaultValue
Returns
Type Constraints
T :struct 
T :IConvertible 

Definition at line 33 of file NameValueCollection.cs.

33  :struct, IConvertible{
34  return ParsePrimitive<T>(q[name],defaultValue);
35  }
static T ParsePrimitive< T >(string value, T?defaultValue)
Provides culture-invariant parsing of int, double, float, bool, and enum values.
static T ImageResizer.ExtensionMethods.NameValueCollectionExtensions.Get< T > ( NameValueCollection  q,
string  name,
defaultValue 
)
inlinestatic

Provides culture-invariant parsing of int, double, float, bool, and enum values.

Template Parameters
T
Parameters
q
name
defaultValue
Returns
Type Constraints
T :struct 
T :IConvertible 

Definition at line 44 of file NameValueCollection.cs.

44  : struct, IConvertible {
45  return ParsePrimitive<T>(q[name], defaultValue).Value;
46  }
static T ParsePrimitive< T >(string value, T?defaultValue)
Provides culture-invariant parsing of int, double, float, bool, and enum values.
static bool ImageResizer.ExtensionMethods.NameValueCollectionExtensions.IsOneSpecified ( NameValueCollection  q,
params string[]  keys 
)
inlinestatic

Returns true if any of the specified keys contain a value

Parameters
q
keys
Returns

Definition at line 202 of file NameValueCollection.cs.

202  {
203  foreach (String s in keys) if (!string.IsNullOrEmpty(q[s])) return true;
204  return false;
205  }
static NameValueCollection ImageResizer.ExtensionMethods.NameValueCollectionExtensions.Keep ( NameValueCollection  q,
params string[]  keysToKeep 
)
inlinestatic

Creates and returns a new NameValueCollection instance that contains only the specified keys from the current collection.

Parameters
q
keysToKeep
Returns

Definition at line 239 of file NameValueCollection.cs.

239  {
240  NameValueCollection c = new NameValueCollection();
241  foreach (string s in keysToKeep)
242  if (q[s] != null) c[s] = q[s];
243 
244  return c;
245  }
static NameValueCollection ImageResizer.ExtensionMethods.NameValueCollectionExtensions.Normalize ( NameValueCollection  q,
string  primary,
string  secondary 
)
inlinestatic

Normalizes a command that has two possible names. If either of the commands has a null or empty value, those keys are removed. If both the the primary and secondary are present, the secondary is removed. Otherwise, the secondary is renamed to the primary name.

Parameters
primary
secondary

Definition at line 217 of file NameValueCollection.cs.

217  {
218  //Get rid of null and empty values.
219  if (string.IsNullOrEmpty(q[primary])) q.Remove(primary);
220  if (string.IsNullOrEmpty(q[secondary])) q.Remove(secondary);
221  //Our job is done if no secondary value exists.
222  if (q[secondary] == null) return q;
223  else {
224  //Otherwise, we have to resolve it
225  //No primary value? copy the secondary one. Otherwise leave it be
226  if (q[primary] == null) q[primary] = q[secondary];
227  //In either case, we now have a duplicate to remove
228  q.Remove(secondary);
229  }
230  return q;
231  }
static T [] ImageResizer.ExtensionMethods.NameValueCollectionExtensions.ParseList< T > ( string  text,
T?  fallbackValue,
params int[]  allowedSizes 
)
inlinestatic

Parses a comma-delimited list of primitive values. If there are unparsable items in the list, they will be replaced with 'fallbackValue'. If fallbackValue is null, the function will return null

Template Parameters
T
Parameters
text
fallbackValue
allowedSizes
Returns
Type Constraints
T :struct 
T :IConvertible 

Definition at line 149 of file NameValueCollection.cs.

149  : struct, IConvertible {
150  if (text == null) return null;
151  text = text.Trim(' ', '(', ')', ','); //Trim parenthesis, commas, and spaces
152  if (text.Length == 0) return null;
153 
154  string[] parts = text.Split(new char[] { ',' }, StringSplitOptions.None);
155 
156  //Verify the array is of an accepted size if any are specified
157  bool foundCount = allowedSizes.Length == 0;
158  foreach (int c in allowedSizes) if (c == parts.Length) foundCount = true;
159  if (!foundCount) return null;
160 
161  //Parse the array
162  var vals = new T[parts.Length];
163  for (var i = 0; i < parts.Length; i++) {
164  var v = ParsePrimitive<T>(parts[i], fallbackValue);
165  if (v == null) return null;
166  vals[i] = v.Value;
167  }
168  return vals;
169  }
static T ParsePrimitive< T >(string value, T?defaultValue)
Provides culture-invariant parsing of int, double, float, bool, and enum values.
static T ImageResizer.ExtensionMethods.NameValueCollectionExtensions.ParsePrimitive< T > ( string  value,
T?  defaultValue 
)
inlinestatic

Provides culture-invariant parsing of int, double, float, bool, and enum values.

Template Parameters
T
Parameters
value
defaultValue
Returns
Type Constraints
T :struct 
T :IConvertible 

Definition at line 56 of file NameValueCollection.cs.

56  : struct,IConvertible {
57  if (value == null) return defaultValue;
58  value = value.Trim(); //Trim whitespace
59  if (value.Length == 0) return defaultValue;
60 
61  Type t = typeof(T);
62 
63  if (t == typeof(byte)) {
64  byte temp = 0;
65  if (byte.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out temp)) return temp as T?;
66  } else if (t == typeof(int)) {
67  int temp = 0;
68  if (int.TryParse(value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out temp)) return temp as T?;
69  } else if (t == typeof(double)) {
70  double temp = 0;
71  if (double.TryParse(value, floatingPointStyle, NumberFormatInfo.InvariantInfo, out temp)) return temp as T?;
72  } else if (t == typeof(float)) {
73  float temp = 0;
74  if (float.TryParse(value, floatingPointStyle, NumberFormatInfo.InvariantInfo, out temp)) return temp as T?;
75  } else if (t == typeof(bool)) {
76  string s = value;
77  if ("true".Equals(s, StringComparison.OrdinalIgnoreCase) ||
78  "1".Equals(s, StringComparison.OrdinalIgnoreCase) ||
79  "yes".Equals(s, StringComparison.OrdinalIgnoreCase) ||
80  "on".Equals(s, StringComparison.OrdinalIgnoreCase)) return true as T?;
81  else if ("false".Equals(s, StringComparison.OrdinalIgnoreCase) ||
82  "0".Equals(s, StringComparison.OrdinalIgnoreCase) ||
83  "no".Equals(s, StringComparison.OrdinalIgnoreCase) ||
84  "off".Equals(s, StringComparison.OrdinalIgnoreCase)) return false as T?;
85  } else if (t.IsEnum) {
86  T? val = EnumExtensions.Parse<T>(value); //Support EnumString values
87  if (val.HasValue) return val;
88  } else {
89  T? val = value as T?; //Just try casting
90  if (val.HasValue) return val;
91  }
92 
93  return defaultValue;
94  }
static NameValueCollection ImageResizer.ExtensionMethods.NameValueCollectionExtensions.Set< T > ( NameValueCollection  q,
string  name,
T?  val 
)
inlinestatic

Provides culture-invariant serialization of value types, in lower case for querystring readability. Setting a key to null removes it.

Template Parameters
T
Parameters
q
name
val
Returns
Type Constraints
T :struct 
T :IConvertible 

Definition at line 131 of file NameValueCollection.cs.

131  : struct, IConvertible {
132  if (val == null) q.Remove(name);
133  else q[name] = SerializePrimitive<T>(val);
134  return q;
135  }
static NameValueCollection ImageResizer.ExtensionMethods.NameValueCollectionExtensions.SetAsString< T > ( NameValueCollection  q,
string  name,
val 
)
inlinestatic

Serializes the given value by calling .ToString(). If the value is null, the key is removed.

Template Parameters
T
Parameters
q
name
val
Returns
Type Constraints
T :class 

Definition at line 117 of file NameValueCollection.cs.

117  : class {
118  if (val == null) q.Remove(name);
119  else q[name] = val.ToString();
120  return q;
121  }

The documentation for this class was generated from the following file: