public IndexValue Read(BinaryReader reader) { var type = (IndexValueType)reader.ReadByte(); switch (type) { case IndexValueType.Null: return(new IndexValue()); case IndexValueType.DateTime: return(new IndexValue(DateTime.FromBinary(reader.ReadInt64()))); case IndexValueType.Integer: return(new IndexValue(reader.ReadInt32())); case IndexValueType.Number: return(new IndexValue(reader.ReadDouble())); case IndexValueType.String: return(new IndexValue(reader.ReadString())); case IndexValueType.Bool: return(new IndexValue(reader.ReadBoolean())); default: Verify.Argument(true, "Illegal tag found in index"); throw null; } }
/// <summary>Gets a client.</summary> /// <param name="clientName">The name of the client.</param> /// <param name="creator">The function to create the client.</param> /// <returns>The client.</returns> /// <exception cref="ArgumentNullException"><paramref name="clientName"/> or <paramref name="creator "/>is <see langword="null"/>.</exception> /// <exception cref="ArgumentException"><paramref name="clientName"/> is empty.</exception> public HttpClient Get(string clientName, Func <HttpClient> creator) { Verify.Argument(nameof(clientName)).WithValue(clientName).IsNotNullOrEmpty(); Verify.Argument(nameof(creator)).WithValue(creator).IsNotNull(); return(GetCore(clientName, creator)); }
public void Write(BinaryWriter writer, IndexValue value) { writer.Write((byte)value.Type); switch (value.Type) { case IndexValueType.Null: break; case IndexValueType.DateTime: writer.Write(((DateTime)value.Value).ToBinary()); break; case IndexValueType.Integer: writer.Write((Int32)value.Value); break; case IndexValueType.Number: writer.Write((double)value.Value); break; case IndexValueType.String: writer.Write((string)value.Value); break; case IndexValueType.Bool: writer.Write((bool)value.Value); break; default: Verify.Argument(true, "Attempt to index illegal value {0}", value.Value); throw null; } }
public int Compare(IndexValue x, IndexValue y) { if (x.Type == y.Type) { switch (x.Type) { case IndexValueType.Null: return(0); case IndexValueType.DateTime: return(DateTimeComparer.Compare((DateTime)x.Value, (DateTime)y.Value)); case IndexValueType.Integer: return(IntegerComparer.Compare((Int32)x.Value, (Int32)y.Value)); case IndexValueType.Number: return(NumberComparer.Compare((double)x.Value, (double)y.Value)); case IndexValueType.String: return(StringComparer.Compare((string)x.Value, (string)y.Value)); case IndexValueType.Bool: return(BoolComparer.Compare((bool)x.Value, (bool)y.Value)); default: Verify.Argument(true, "Attempt to compare illegal values, {0} and {1}", x.Value, y.Value); throw null; } } return(Comparer <int> .Default.Compare((int)x.Type, (int)y.Type)); }
public QueryPath(string queryPath) { Verify.Argument(nameof(queryPath)).WithValue(queryPath).IsNotNullOrEmpty(); FullPath = queryPath.Trim(s_nodePathDelimiters); _normalizedPath = new Lazy <(string Name, QueryPath Parent)>(NormalizePath); }
public NodePath(string nodePath) { Verify.Argument(nameof(nodePath)).WithValue(nodePath).IsNotNullOrEmpty(); FullPath = nodePath; _normalizedPath = new Lazy <(string Project, string Name, NodePath Parent)>(NormalizePath); }
/// <summary>Adds paging to a query.</summary> /// <param name="source">The source value.</param> /// <param name="pageIndex">The zero-based index of the page.</param> /// <param name="pageSize">The size of a page.</param> /// <returns>The query.</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="pageIndex"/> is less than zero. /// <para>-or-</para> /// <paramref name="pageSize"/> is less than one. /// </exception> public static IEnumerable <T> WithPaging <T> (this IEnumerable <T> source, int pageIndex, int pageSize) { Verify.Argument(nameof(pageIndex)).WithValue(pageIndex).IsGreaterThanOrEqualToZero(); Verify.Argument(nameof(pageSize)).WithValue(pageSize).IsGreaterThanZero(); return(source.Skip(pageIndex * pageSize).Take(pageSize)); }
/// <summary>Initializes an instance of the <see cref="DataTransaction"/> class.</summary> /// <param name="innerTransaction">The inner transaction object.</param> /// <param name="closeConnection"><see langword="true"/> to close the connection when the transaction is completed.</param> /// <exception cref="ArgumentNullException"><paramref name="innerTransaction"/> is <see langword="null"/>.</exception> public DataTransaction(DbTransaction innerTransaction, bool closeConnection) { Verify.Argument("innerTransaction", innerTransaction).IsNotNull(); m_inner = innerTransaction; m_autoClose = closeConnection; }
/// <summary>Initializes an instance of the <see cref="DataCommand"/> class.</summary> /// <param name="commandText">The command text.</param> /// <param name="type">The type of command.</param> /// <exception cref="ArgumentNullException"><paramref name="commandText"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException"><paramref name="commandText"/> is empty.</exception> public DataCommand(string commandText, CommandType type) { Verify.Argument(nameof(commandText)).WithValue(commandText).IsNotNullOrEmpty(); CommandText = commandText; CommandType = type; }
/// <summary>Gets a client.</summary> /// <param name="clientName">The name of the client.</param> /// <param name="clientUri">The client URL.</param> /// <param name="handler">The optional message handler.</param> /// <returns>The client.</returns> /// <exception cref="ArgumentNullException"><paramref name="clientName"/> or <paramref name="clientUri"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException"><paramref name="clientName"/> is empty.</exception> public HttpClient Get(string clientName, Uri clientUri, HttpMessageHandler handler) { Verify.Argument(nameof(clientName)).WithValue(clientName).IsNotNullOrEmpty(); Verify.Argument(nameof(clientUri)).WithValue(clientUri).IsNotNull(); return(GetCore(clientName, () => CreateCore(clientUri, handler))); }
/// <summary>Edits the property value.</summary> /// <param name="context">The context of the editor.</param> /// <param name="provider">The provider for the editor.</param> /// <param name="value">The current value.</param> /// <returns>The new value.</returns> /// <exception cref="ArgumentNullException"><paramref name="provider"/> is <see langword="null"/>.</exception> public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { Verify.Argument("provider", provider).IsNotNull(); //Generate a drop-down list m_Svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService; if (m_Svc != null) { try { ListBox box = new ListBox(); box.Sorted = Sorted; box.BorderStyle = BorderStyle.None; box.Click += OnBoxClicked; //Get the list of legal values box.Items.AddRange(GetListValues(context, provider, value)); box.SelectedItem = value; m_Svc.DropDownControl(box); return(box.SelectedItem ?? value); } finally { m_Svc = null; }; } ; return(value); }
public CompensatingOperationBehavior(IOperation operation, params Type[] compensatedExceptionTypes) { Verify.NotNull(operation, nameof(operation)); Verify.Argument(compensatedExceptionTypes.All(t => typeof(Exception).IsAssignableFrom(t)), "Only exception types are valid."); _operation = operation; _compensatedExceptionTypes = compensatedExceptionTypes; }
/// <summary>Gets a connection string given its name.</summary> /// <param name="name">The name of the connection string.</param> /// <returns>The connection string or <see langword="null"/> if the connection string is not found.</returns> /// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception> public string GetConnectionString(string name) { Verify.Argument(nameof(name)).WithValue(name).IsNotNullOrEmpty(); var settings = ConfigurationManager.ConnectionStrings[name]; return(settings?.ConnectionString ?? null); }
/// <summary>Removes a client.</summary> /// <param name="clientName">The client name.</param> /// <remarks> /// The client is cleaned up and removed from the factory. /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="clientName"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException"><paramref name="clientName"/> is empty.</exception> public void Remove(string clientName) { Verify.Argument(nameof(clientName)).WithValue(clientName).IsNotNullOrEmpty(); var client = s_clients.Remove(clientName) as HttpClient; SafeDispose(client); }
public void Argument_WithName() { var expectedName = "Test"; var target = Verify.Argument(expectedName); target.Name.Should().Be(expectedName); }
/// <summary> /// Creates a new instance of the attribute. /// </summary> /// <param name="operationType">The type of compensating operation to create in /// case of exceptions. The type must implement the IOperation interface.</param> /// <param name="compensatedExceptionTypes">Optionally supply specific exception types /// to limit the compensating operations to. By default, all exceptions are compensated /// for. Exceptions inheriting from one of the specified types will be compensated for /// as well. The types must inherit from Exception.</param> public CompensatingOperationAttribute(Type operationType, params Type[] compensatedExceptionTypes) { Verify.NotNull(operationType, nameof(operationType)); Verify.Argument(typeof(IOperation).IsAssignableFrom(operationType), "The operation type must implement the IOperation interface"); _operationType = operationType; _compensatedExceptionTypes = compensatedExceptionTypes; }
/// <summary>Initializes an instance of the <see cref="TextTokenInfo"/> structure.</summary> /// <param name="kind">The type of token.</param> /// <param name="text">The token text.</param> /// <param name="position">The position within the text where the token appears.</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="position"/> is less than zero.</exception> public TextTokenInfo(TextTokenKind kind, string text, int position) : this() { Verify.Argument("position", position).IsGreaterThanOrEqualToZero(); Kind = kind; OriginalText = text ?? ""; Position = position; }
/// <summary>Invokes a service method.</summary> /// <typeparam name="TChannel">The service interface.</typeparam> /// <typeparam name="TResult">The return obj from the method.</typeparam> /// <param name="invocation">The function to execute.</param> /// <returns>The result of the method invocation.</returns> /// <exception cref="ArgumentNullException"><paramref name="invocation"/> is <see langword="null"/>.</exception> /// <remarks> /// This method creates a service client, invokes a method and closes the client. It is useful for invoking a single /// method without the boiler plate code. Do not use this method if multiple methods need to be called /// on the same service instance. /// </remarks> public static TResult InvokeMethod <TChannel, TResult> (Func <TChannel, TResult> invocation) where TChannel : class { Verify.Argument("invocation", invocation).IsNotNull(); using (var proxy = CreateAndWrap <TChannel>()) { return(invocation(proxy.Client)); }; }
/// <summary>Invokes a service method.</summary> /// <typeparam name="TChannel">The service interface.</typeparam> /// <param name="invocation">The function to execute.</param> /// <exception cref="ArgumentNullException"><paramref name="invocation"/> is <see langword="null"/>.</exception> /// <remarks> /// This method creates a service client, invokes a method and closes the client. It is useful for invoking a single /// method without the boiler plate code. Do not use this method if multiple methods need to be called /// on the same service instance. /// </remarks> public static void InvokeMethod <TChannel> (Action <TChannel> invocation) where TChannel : class { Verify.Argument("invocation", invocation).IsNotNull(); using (var proxy = CreateAndWrap <TChannel>()) { invocation(proxy.Client); }; }
/// <summary>Posts data as a JSON object.</summary> /// <typeparam name="TRequest">The type of the data being sent.</typeparam> /// <param name="source">The source.</param> /// <param name="requestUri">The URI to send to.</param> /// <param name="content">The data to send.</param> /// <param name="serializerSettings">The optional serializer settings.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The response.</returns> /// <exception cref="ArgumentNullException"><paramref name="requestUri"/> is <see langword="null" />.</exception> /// <preliminary /> public static async Task <HttpResponseMessage> PostJsonAsync <TRequest> (this HttpClient source, Uri requestUri, TRequest content , JsonSerializerSettings serializerSettings, CancellationToken cancellationToken) { Verify.Argument(nameof(requestUri)).WithValue(requestUri).IsNotNull(); var data = await Task.Run(() => serializerSettings != null?JsonConvert.SerializeObject(content, serializerSettings) : JsonConvert.SerializeObject(content)).ConfigureAwait(false); return(await source.PostAsync(requestUri, new StringContent(data, Encoding.UTF8, "application/json"), cancellationToken).ConfigureAwait(false)); }
//TODO: Doesn't make sense to create a command with a specific weapon, should be parameterized data to execute public WeaponAttackCommand(GameItem weapon, int minDamage, int maxDamage) { Verify.Argument(nameof(weapon)).WithValue(weapon).IsNotNull().And.Is(x => x.IsWeapon(), "Must be a weapon"); Verify.Argument(nameof(minDamage)).WithValue(minDamage).IsGreaterThanOrEqualToZero(); Verify.Argument(nameof(maxDamage)).WithValue(maxDamage).IsGreaterThanOrEqualTo(minDamage); _weapon = weapon; _minDmg = minDamage; _maxDmg = maxDamage; }
public void Argument_WithNameAndValue() { var expectedName = "Test"; var expectedValue = Dates.February(4, 2000); var target = Verify.Argument(expectedName, expectedValue); target.Argument.Name.Should().Be(expectedName); target.Argument.Value.Should().Be(expectedValue); }
//TODO: Heal value should be an attribute of the item public HealCommand(GameItem item, int minHeal, int maxHeal) { Verify.Argument(nameof(item)).WithValue(item).IsNotNull(); Verify.Argument(nameof(minHeal)).WithValue(minHeal).IsGreaterThanOrEqualToZero(); Verify.Argument(nameof(maxHeal)).WithValue(maxHeal).IsGreaterThanOrEqualTo(minHeal); _item = item; _minHeal = minHeal; _maxHeal = maxHeal; }
public RetryBehavior(int timesToRetry, TimeSpan retryDelay, params Type[] retryExceptionTypes) { Verify.LargerThanZero(timesToRetry, "Must be larger than 0."); Verify.LargeThanOrEqualToZero(retryDelay.Ticks, "Delay must be non-negative."); Verify.Argument(retryExceptionTypes.All(t => typeof(Exception).IsAssignableFrom(t)), "Only exception types are valid."); RetryExceptionTypes = retryExceptionTypes; TimesToRetry = timesToRetry; RetryDelay = retryDelay; }
/// <summary>Initializes an instance of the <see cref="DbProviderFactoryConnectionManager"/> class.</summary> /// <param name="factory">The underlying factory to use.</param> /// <param name="connectionString">The connection string to use.</param> /// <param name="configurationProvider">The configuration provider.</param> /// <exception cref="ArgumentNullException"><paramref name="factory"/> is <see langword="null"/>.</exception> public DbProviderFactoryConnectionManager(DbProviderFactory factory, string connectionString, IDataConfigurationProvider configurationProvider) : base(connectionString, configurationProvider) { Verify.Argument(nameof(factory)).WithValue(factory).IsNotNull(); Factory = factory; m_schema = new Lazy <SchemaInformation>(CallLoadSchema); ConnectionString = connectionString; }
public static IEnumerable <T> RemoveIf <T> (this IEnumerable <T> source, Func <T, bool> condition) { Verify.Argument(nameof(condition)).WithValue(condition).IsNotNull(); if (source != null) { return(source.Where(x => !condition(x))); } return(Enumerable.Empty <T>()); }
/// <summary>Gets a connection string given its name.</summary> /// <param name="name">The name of the connection string.</param> /// <returns>The connection string or <see langword="null"/> if the connection string is not found.</returns> /// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException"><paramref name="name"/> is empty.</exception> public string GetConnectionString(string name) { Verify.Argument(nameof(name)).WithValue(name).IsNotNullOrEmpty(); if (ConnectionStrings.TryGetValue(name, out var connString)) { return(connString); } return(null); }
int IEqualityComparer.GetHashCode(object value) { Verify.Argument("value", value).IsNotNull(); if (value is char) { return(GetHashCode((char)value)); } return(value.GetHashCode()); }
/// <summary>Calculates the check digit given a value.</summary> /// <param name="value">The value.</param> /// <returns>The check digit (between 0 and 9).</returns> /// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException"><paramref name="value"/> is empty or invalid.</exception> public static int CalculateCheckDigit(string value) { Verify.Argument("value", value).IsNotNullOrEmpty().And.IsNumeric(); //Sum digits var sum = GetChecksum(value + '0'); var mod = sum % 10; return((mod == 0) ? (int)0 : (int)(10 - mod)); }
public static IEnumerable <T> ForEach <T> (this IEnumerable <T> source, Action <T> action) { Verify.Argument(nameof(action)).WithValue(action).IsNotNull(); foreach (var item in source) { action(item); } ; return(source); }