本文整理汇总了C#中ImmutableArray.FirstOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# ImmutableArray.FirstOrDefault方法的具体用法?C# ImmutableArray.FirstOrDefault怎么用?C# ImmutableArray.FirstOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ImmutableArray
的用法示例。
在下文中一共展示了ImmutableArray.FirstOrDefault方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FormatDiagnostics
/// <summary>
/// Helper method to format a <see cref="Diagnostic"/> into an easily readable string.
/// </summary>
/// <param name="analyzers">The analyzers that this verifier tests.</param>
/// <param name="diagnostics">A collection of <see cref="Diagnostic"/>s to be formatted.</param>
/// <returns>The <paramref name="diagnostics"/> formatted as a string.</returns>
private static string FormatDiagnostics(ImmutableArray<DiagnosticAnalyzer> analyzers, params Diagnostic[] diagnostics)
{
var builder = new StringBuilder();
for (int i = 0; i < diagnostics.Length; ++i)
{
var diagnosticsId = diagnostics[i].Id;
builder.AppendLine("// " + diagnostics[i].ToString());
var applicableAnalyzer = analyzers.FirstOrDefault(a => a.SupportedDiagnostics.Any(dd => dd.Id == diagnosticsId));
if (applicableAnalyzer != null)
{
var analyzerType = applicableAnalyzer.GetType();
var location = diagnostics[i].Location;
if (location == Location.None)
{
builder.AppendFormat("GetGlobalResult({0}.{1})", analyzerType.Name, diagnosticsId);
}
else
{
Assert.True(
location.IsInSource,
string.Format("Test base does not currently handle diagnostics in metadata locations. Diagnostic in metadata:\r\n", diagnostics[i]));
string resultMethodName = diagnostics[i].Location.SourceTree.FilePath.EndsWith(".cs") ? "GetCSharpResultAt" : "GetBasicResultAt";
var linePosition = diagnostics[i].Location.GetLineSpan().StartLinePosition;
builder.AppendFormat(
"{0}({1}, {2}, {3}.{4})",
resultMethodName,
linePosition.Line + 1,
linePosition.Character + 1,
analyzerType.Name,
diagnosticsId);
}
if (i != diagnostics.Length - 1)
{
builder.Append(',');
}
builder.AppendLine();
}
}
return builder.ToString();
}
示例2: WithFilteredItems
public Model WithFilteredItems(ImmutableArray<PresentationItem> filteredItems)
{
return With(filteredItems: filteredItems, selectedItem: filteredItems.FirstOrDefault());
}
示例3: GetBestCompletionItemBasedOnMRU
/// <summary>
/// Given multiple possible chosen completion items, pick the one that has the
/// best MRU index.
/// </summary>
private CompletionItem GetBestCompletionItemBasedOnMRU(
ImmutableArray<CompletionItem> chosenItems, ImmutableArray<string> recentItems)
{
var bestItem = chosenItems.FirstOrDefault();
for (int i = 1, n = chosenItems.Length; i < n; i++)
{
var chosenItem = chosenItems[i];
var mruIndex1 = GetRecentItemIndex(recentItems, bestItem);
var mruIndex2 = GetRecentItemIndex(recentItems, chosenItem);
if (mruIndex2 < mruIndex1)
{
bestItem = chosenItem;
}
}
return bestItem;
}
示例4: CheckIfAnalyzerThrew
/// <param name="diags">The compiler diagnostics at a given compilation.</param>
/// <remarks>
/// Todo: Remove/Update when https://github.com/dotnet/roslyn/issues/2580 is completed and there is
/// an api to check for analyzer exceptions
/// </remarks>
private static void CheckIfAnalyzerThrew(ImmutableArray<Diagnostic> diags)
{
var exceptionAnalyzer = diags.FirstOrDefault(d => d.Id == "AD0001");
if (exceptionAnalyzer != null) throw new Exception($"Analyzer threw. Details:\nMessage:{exceptionAnalyzer.GetMessage()}.");
}
示例5: TryToBind
/// <summary>
///
/// </summary>
/// <param name="bindings"></param>
/// <param name="db"></param>
/// <param name="propertyName"></param>
/// <param name="value"></param>
/// <returns></returns>
private static bool TryToBind(
ImmutableArray<ShapeBinding> bindings,
ImmutableArray<ShapeProperty> db,
string propertyName,
out string value)
{
foreach (var binding in bindings)
{
if (string.IsNullOrEmpty(binding.Property) || string.IsNullOrEmpty(binding.Path))
continue;
if (binding.Property != propertyName)
continue;
var result = db.FirstOrDefault(p => p.Name == binding.Path);
if (result != null && result.Value != null)
{
value = result.Value.ToString();
return true;
}
}
value = null;
return false;
}
示例6: GetFirstMatchingNamespaceOrType
private static INamespaceOrTypeSymbol GetFirstMatchingNamespaceOrType(ImmutableArray<ISymbol> candidateMembers)
{
return (INamespaceOrTypeSymbol)candidateMembers
.FirstOrDefault(s =>
s.Kind == SymbolKind.Namespace ||
s.Kind == SymbolKind.NamedType);
}
示例7: GetFirstMatchingNamedType
private static INamedTypeSymbol GetFirstMatchingNamedType(ImmutableArray<ISymbol> candidateMembers, int arity)
{
return (INamedTypeSymbol)candidateMembers.FirstOrDefault(s =>
s.Kind == SymbolKind.NamedType &&
((INamedTypeSymbol)s).Arity == arity);
}
示例8: WithFilteredItems
public Model WithFilteredItems(ImmutableArray<PresentationItem> filteredItems)
{
return new Model(_disconnectedBufferGraph, OriginalList, TotalItems, filteredItems,
filteredItems.FirstOrDefault(), CompletionItemFilters, FilterState, CompletionItemToFilterText, IsHardSelection,
IsUnique, UseSuggestionMode, SuggestionModeItem, DefaultSuggestionModeItem,
Trigger, CommitTrackingSpanEndPoint, DismissIfEmpty);
}
示例9: GetBestCompletionItemBasedOnMRU
/// <summary>
/// Given multiple possible chosen completion items, pick the one that has the
/// best MRU index.
/// </summary>
private CompletionItem GetBestCompletionItemBasedOnMRU(
ImmutableArray<CompletionItem> chosenItems, ImmutableArray<string> recentItems)
{
if (chosenItems.Length == 0)
{
return null;
}
// Try to find the chosen item has been most
// recently used.
var bestItem = chosenItems.FirstOrDefault();
for (int i = 0, n = chosenItems.Length; i < n; i++)
{
var chosenItem = chosenItems[i];
var mruIndex1 = GetRecentItemIndex(recentItems, bestItem);
var mruIndex2 = GetRecentItemIndex(recentItems, chosenItem);
if (mruIndex2 < mruIndex1)
{
bestItem = chosenItem;
}
}
// If our best item appeared in the MRU list, use it
if (GetRecentItemIndex(recentItems, bestItem) <= 0)
{
return bestItem;
}
// Otherwise use the chosen item that has the highest
// matchPriority.
for (int i = 1, n = chosenItems.Length; i < n; i++)
{
var chosenItem = chosenItems[i];
var bestItemPriority = bestItem.Rules.MatchPriority;
var currentItemPriority = chosenItem.Rules.MatchPriority;
if (currentItemPriority > bestItemPriority)
{
bestItem = chosenItem;
}
}
return bestItem;
}
示例10: TryToBind
/// <summary>
/// Try binding properties array to one of <see cref="XText"/> shape properties.
/// </summary>
/// <param name="db">The properties database used for binding.</param>
/// <param name="propertyName">The target property name.</param>
/// <param name="value">The string bound to properties.</param>
/// <returns>True if binding was successful.</returns>
private static bool TryToBind(ImmutableArray<XProperty> db, string propertyName, out string value)
{
if (string.IsNullOrEmpty(propertyName) || db == null)
{
value = null;
return false;
}
var result = db.FirstOrDefault(p => p.Name == propertyName);
if (result != null && result.Value != null)
{
value = result.Value.ToString();
return true;
}
value = null;
return false;
}