本文整理汇总了C#中System.Collections.Generic.AsEnumerable方法的典型用法代码示例。如果您正苦于以下问题:C# System.Collections.Generic.AsEnumerable方法的具体用法?C# System.Collections.Generic.AsEnumerable怎么用?C# System.Collections.Generic.AsEnumerable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Generic
的用法示例。
在下文中一共展示了System.Collections.Generic.AsEnumerable方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AnonymousType
public void AnonymousType()
{
var list = new[] {
new { FirstName = "Jon", Surname = "Skeet" },
new { FirstName = "Holly", Surname = "Skeet" }
}.ToList();
// We can't cast to IEnumerable<T> as we can't express T.
var sequence = list.AsEnumerable();
// This will now use Enumerable.Contains instead of List.Contains
Assert.IsFalse(sequence.Contains(new { FirstName = "Tom", Surname = "Skeet" }));
}
示例2: FormatMessage
public void FormatMessage()
{
const string Pattern = "{name} has {messages, plural, 123}.";
const string Expected = "Jeff has 123 messages.";
var args = new Dictionary<string, object> { { "name", "Jeff" }, { "messages", 1 } };
var requests = new[]
{
new FormatterRequest(
new Literal(0, 5, 1, 7, new StringBuilder("name")),
"name",
null,
null),
new FormatterRequest(
new Literal(11, 33, 1, 7, new StringBuilder("messages, plural, 123")),
"messages",
"plural",
" 123")
};
this.formatterMock1.Setup(x => x.Format("en", requests[0], args, "Jeff", this.subject)).Returns("Jeff");
this.formatterMock2.Setup(x => x.Format("en", requests[1], args, 1, this.subject)).Returns("123 messages");
this.collectionMock.Setup(x => x.GetEnumerator()).Returns(requests.AsEnumerable().GetEnumerator());
this.libraryMock.Setup(x => x.GetFormatter(requests[0])).Returns(this.formatterMock1.Object);
this.libraryMock.Setup(x => x.GetFormatter(requests[1])).Returns(this.formatterMock2.Object);
this.patternParserMock.Setup(x => x.Parse(It.IsAny<StringBuilder>())).Returns(this.collectionMock.Object);
// First request, and "name" is 4 chars.
this.collectionMock.Setup(x => x.ShiftIndices(0, 4)).Callback(
// The '- 2' is also done in the used implementation.
(int index, int length) => requests[1].SourceLiteral.ShiftIndices(length - 2, requests[0].SourceLiteral));
var actual = this.subject.FormatMessage(Pattern, args);
this.collectionMock.Verify(x => x.ShiftIndices(0, 4), Times.Once);
this.libraryMock.VerifyAll();
this.formatterMock1.VerifyAll();
this.formatterMock2.VerifyAll();
this.patternParserMock.VerifyAll();
Assert.Equal(Expected, actual);
}
示例3: VerifyFormatMessageThrowsWhenVariablesAreMissing
public void VerifyFormatMessageThrowsWhenVariablesAreMissing()
{
const string Pattern = "{name} has {messages, plural, 123}.";
// Note the missing "name" variable.
var args = new Dictionary<string, object> { { "messages", 1 } };
var requests = new[]
{
new FormatterRequest(
new Literal(0, 5, 1, 7, new StringBuilder("name")),
"name",
null,
null),
new FormatterRequest(
new Literal(11, 33, 1, 7, new StringBuilder("messages, plural, 123")),
"messages",
"plural",
" 123")
};
this.collectionMock.Setup(x => x.GetEnumerator()).Returns(() => requests.AsEnumerable().GetEnumerator());
this.patternParserMock.Setup(x => x.Parse(It.IsAny<StringBuilder>())).Returns(this.collectionMock.Object);
// First request, and "name" is 4 chars.
this.collectionMock.Setup(x => x.ShiftIndices(0, 4)).Callback(
// The '- 2' is also done in the used implementation.
(int index, int length) => requests[1].SourceLiteral.ShiftIndices(length - 2, requests[0].SourceLiteral));
var ex = Assert.Throws<VariableNotFoundException>(() => this.subject.FormatMessage(Pattern, args));
Assert.Equal("name", ex.MissingVariable);
}