update MEF to preview 9
[mcs.git] / class / System.ComponentModel.Composition / src / ComponentModel / Microsoft / Internal / Requires.cs
blobfa9656c544f18eec80d7d8d63c41f0f54ca8bc27
1 // -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
4 using System;
5 using System.Collections;
6 using System.Collections.Generic;
7 using System.Diagnostics;
8 using System.Linq;
9 using System.Globalization;
10 using System.Reflection;
11 using System.ComponentModel.Composition;
12 using System.Text;
14 namespace Microsoft.Internal
16 internal static class Requires
18 [DebuggerStepThrough]
19 public static void NotNull<T>(T value, string parameterName)
20 where T : class
22 if (value == null)
24 throw new ArgumentNullException(parameterName);
28 [DebuggerStepThrough]
29 public static void NotNullOrEmpty(string value, string parameterName)
31 NotNull(value, parameterName);
33 if (value.Length == 0)
35 throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.ArgumentException_EmptyString, parameterName), parameterName);
39 [DebuggerStepThrough]
40 public static void NotNullOrNullElements<T>(IEnumerable<T> values, string parameterName)
41 where T : class
43 NotNull(values, parameterName);
44 NotNullElements(values, parameterName);
47 [DebuggerStepThrough]
48 public static void NullOrNotNullElements<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> values, string parameterName)
49 where TKey : class
50 where TValue : class
52 if (values != null)
54 NotNullElements(values, parameterName);
58 [DebuggerStepThrough]
59 public static void NullOrNotNullElements<T>(IEnumerable<T> values, string parameterName)
60 where T : class
62 if (values != null)
64 NotNullElements(values, parameterName);
68 private static void NotNullElements<T>(IEnumerable<T> values, string parameterName)
69 where T : class
71 foreach (T value in values)
73 if (value == null)
75 throw ExceptionBuilder.CreateContainsNullElement(parameterName);
80 private static void NotNullElements<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> values, string parameterName)
81 where TKey : class
82 where TValue : class
84 foreach (KeyValuePair<TKey, TValue> value in values)
86 if ((value.Key == null) || (value.Value == null))
88 throw ExceptionBuilder.CreateContainsNullElement(parameterName);
92 [DebuggerStepThrough]
93 public static void IsInMembertypeSet(MemberTypes value, string parameterName, MemberTypes enumFlagSet)
95 if ((value & enumFlagSet) != value || // Ensure the member is in the set
96 (value & (value - 1)) != 0) // Ensure that there is only one flag in the value (i.e. value is a power of 2).
98 throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.ArgumentOutOfRange_InvalidEnumInSet, parameterName, value, enumFlagSet.ToString()), parameterName);