本文整理汇总了C#中IDictionary.GetOrAdd方法的典型用法代码示例。如果您正苦于以下问题:C# IDictionary.GetOrAdd方法的具体用法?C# IDictionary.GetOrAdd怎么用?C# IDictionary.GetOrAdd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDictionary
的用法示例。
在下文中一共展示了IDictionary.GetOrAdd方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateTestFile
public static string CreateTestFile(string code, IDictionary<string, IList<TextSpan>> spans, int cursor = -1)
{
var sb = new StringBuilder();
var anonymousSpans = spans.GetOrAdd(string.Empty, () => new List<TextSpan>());
for (int i = 0; i <= code.Length; i++)
{
if (i == cursor)
{
sb.Append(PositionString);
}
AddSpanString(sb, spans.Where(kvp => kvp.Key != string.Empty), i, start: true);
AddSpanString(sb, spans.Where(kvp => kvp.Key == string.Empty), i, start: true);
AddSpanString(sb, spans.Where(kvp => kvp.Key == string.Empty), i, start: false);
AddSpanString(sb, spans.Where(kvp => kvp.Key != string.Empty), i, start: false);
if (i < code.Length)
{
sb.Append(code[i]);
}
}
return sb.ToString();
}
示例2: PopSpan
private static void PopSpan(
Stack<Tuple<int, string>> spanStartStack,
IDictionary<string, IList<TextSpan>> spans,
int finalIndex)
{
var spanStartTuple = spanStartStack.Pop();
var span = TextSpan.FromBounds(spanStartTuple.Item1, finalIndex);
spans.GetOrAdd(spanStartTuple.Item2, () => new List<TextSpan>()).Add(span);
}
示例3: HandleBodyLinks
/// <summary>
/// Handles links embedded in JSON response bodies.
/// </summary>
/// <param name="jsonBody">The body to check for links.</param>
/// <param name="links">A dictionary to add found links to.</param>
/// <param name="linkTemplates">A dictionary to add found link templates to.</param>
protected virtual void HandleBodyLinks(JToken jsonBody, IDictionary<string, Dictionary<Uri, string>> links, IDictionary<string, UriTemplate> linkTemplates)
{
if (jsonBody.Type != JTokenType.Object) return;
var linksNode = jsonBody["_links"] ?? jsonBody["links"];
if (linksNode == null) return;
foreach (var linkNode in linksNode.OfType<JProperty>())
{
string rel = linkNode.Name;
var linksForRel = links.GetOrAdd(rel);
switch (linkNode.Value.Type)
{
case JTokenType.Array:
foreach (var subobj in linkNode.Value.OfType<JObject>())
ParseLinkObject(rel, subobj, linksForRel, linkTemplates);
break;
case JTokenType.Object:
ParseLinkObject(rel, (JObject)linkNode.Value, linksForRel, linkTemplates);
break;
}
}
}
示例4: CommandAttribute
public CommandAttribute(IDictionary<string, object> attributes)
: this((string)attributes.GetOrAdd("PackageId", _ => Guid.Empty.ToString()), (string)attributes["GroupId"], (int)attributes["CommandId"])
{
}
示例5: HandleHeaderLinks
/// <summary>
/// Handles links embedded in HTTP response headers.
/// </summary>
/// <param name="headers">The headers to check for links.</param>
/// <param name="links">A dictionary to add found links to.</param>
/// <param name="linkTemplates">A dictionary to add found link templates to.</param>
protected virtual void HandleHeaderLinks(HttpResponseHeaders headers, IDictionary<string, Dictionary<Uri, string>> links, IDictionary<string, UriTemplate> linkTemplates)
{
foreach (var header in headers.GetLinkHeaders().Where(x => x.Rel != null))
{
if (header.Templated)
linkTemplates[header.Rel] = new UriTemplate(header.Href);
else
links.GetOrAdd(header.Rel)[new Uri(Uri, header.Href)] = header.Title;
}
}