本文整理汇总了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;
}
示例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;
}
示例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);
}