当前位置: 首页>>代码示例>>C#>>正文


C# Collection.SingleOrDefault方法代码示例

本文整理汇总了C#中Collection.SingleOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# Collection.SingleOrDefault方法的具体用法?C# Collection.SingleOrDefault怎么用?C# Collection.SingleOrDefault使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Collection的用法示例。


在下文中一共展示了Collection.SingleOrDefault方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: OnValidate

        /// <summary>
        /// Validates a property whose name matches the specified <see cref="propertyName"/>.
        /// </summary>
        /// <param name="propertyName">The name of the property to validate.</param>
        /// <returns>Returns a validation error, if any, otherwise returns null.</returns>
        protected virtual string OnValidate(string propertyName)
        {
            var context = new ValidationContext(this)
            {
                MemberName = propertyName
            };

            var results = new Collection<ValidationResult>();
            var isValid = Validator.TryValidateObject(this, context, results, true);

            if (isValid) return null;
            var result = results.SingleOrDefault(p =>
                p.MemberNames.Any(memberName =>
                    memberName == propertyName));

            return result?.ErrorMessage;
        }
开发者ID:leovard,项目名称:Istar.ModernUI,代码行数:22,代码来源:ViewModel.cs

示例2: Validate

        /// <summary>
        /// Validates a property whose name matches the specified <see cref="propertyName"/>.
        /// </summary>
        /// <param name="propertyName">The name of the property to validate.</param>
        /// <returns>Returns a validation error, if any, otherwise returns null.</returns>
        protected virtual string Validate(string propertyName)
        {
            ValidationContext validationContext = new ValidationContext(this)
            {
                MemberName = propertyName
            };

            Collection<ValidationResult> validationResults = new Collection<ValidationResult>();

            if (!Validator.TryValidateObject(this, validationContext, validationResults, true))
            {
                ValidationResult validationResult = validationResults.SingleOrDefault(r => r.MemberNames.Any(x => x == propertyName));

                return validationResult == null
                    ? null
                    : validationResult.ErrorMessage;
            }

            return null;
        }
开发者ID:pravdivix,项目名称:demo,代码行数:25,代码来源:ViewModel.cs

示例3: CreateTokenInfo

        /// <inheritdoc />
        public virtual TokenInfo CreateTokenInfo(IEnumerable<Claim> claims, TimeSpan? lifetime, string secretKey)
        {
            if (claims == null)
            {
                throw new ArgumentNullException("claims");
            }

            if (lifetime != null && lifetime < TimeSpan.Zero)
            {
                string msg = CommonResources.ArgMustBeGreaterThanOrEqualTo.FormatForUser(TimeSpan.Zero);
                throw new ArgumentOutOfRangeException("lifetime", lifetime, msg);
            }

            if (string.IsNullOrEmpty(secretKey))
            {
                throw new ArgumentNullException("secretKey");
            }

            // add the claims passed in
            Collection<Claim> finalClaims = new Collection<Claim>();
            foreach (Claim claim in claims)
            {
                finalClaims.Add(claim);
            }

            // add our standard claims
            finalClaims.Add(new Claim("ver", "3"));

            Claim uidClaim = finalClaims.SingleOrDefault(p => p.Type == ClaimTypes.NameIdentifier);
            if (uidClaim != null)
            {
                finalClaims.Remove(uidClaim);
                finalClaims.Add(new Claim("uid", uidClaim.Value));
            }

            return CreateTokenFromClaims(finalClaims, secretKey, ZumoAudienceValue, ZumoIssuerValue, lifetime);
        }
开发者ID:huoxudong125,项目名称:azure-mobile-apps-net-server,代码行数:38,代码来源:MobileAppTokenHandler.cs


注:本文中的Collection.SingleOrDefault方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。