本文整理汇总了C#中System.Collections.Generic.System.Collections.Generic.Dictionary.TryGetValue方法的典型用法代码示例。如果您正苦于以下问题:C# System.Collections.Generic.Dictionary.TryGetValue方法的具体用法?C# System.Collections.Generic.Dictionary.TryGetValue怎么用?C# System.Collections.Generic.Dictionary.TryGetValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Generic.System.Collections.Generic.Dictionary
的用法示例。
在下文中一共展示了System.Collections.Generic.Dictionary.TryGetValue方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
string errorCode = string.Empty;
string res = string.Empty;
string SourceCountryName = string.Empty;
string TargetCountryName = string.Empty;
string InputAmt = string.Empty;
string OutputAmt = string.Empty;
string jsonRes = string.Empty;
Dictionary<string, string> values = new System.Collections.Generic.Dictionary<string,string>();
string RawUrlx = "http://localhost/currency/?from=USD&to=EUR&q=1";
NameValueCollection nm = HttpUtility.ParseQueryString(RawUrlx.Substring(RawUrlx.IndexOf("/?") + 2));
SourceCountryName = nm.Get("from");
TargetCountryName = nm.Get("to");
InputAmt = nm.Get("q");
nm.Add("ConvertedAmt",string.Empty);
//Do All the good stuff here - validation
var requestUri = string.Format("http://rate-exchange.appspot.com/currency?from={0}&to={1}&q={2}", Uri.EscapeDataString(SourceCountryName), Uri.EscapeDataString(TargetCountryName), Uri.EscapeDataString("1.23x"));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
jsonRes = reader.ReadToEnd();
values = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonRes);
}
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
}
throw;
}
values.TryGetValue("err", out errorCode);
values.TryGetValue("warning", out errorCode);
// if (error)
// return errorCode;
bool successfulConv = values.TryGetValue("v", out OutputAmt);
}
示例2: UpdateParts
protected virtual void UpdateParts(ContentItem target, ContentItem source)
{
List<string> keysTarget = new List<string>(target.GetChildren(new Collections.PartFilter()).Keys);
List<string> keysSource = new List<string>(source.GetChildren(new Collections.PartFilter()).Keys);
System.Collections.Generic.Dictionary<int, ContentItem> toCopy = new System.Collections.Generic.Dictionary<int, ContentItem>();
bool bDelete = true;
bool bAdd = true;
foreach (string keyTarget in keysTarget)
{
ContentItem childTarget = target.Children[keyTarget];
foreach (string keySource in keysSource)
{
ContentItem childSource = source.Children[keySource];
if (childSource.TranslationKey == childTarget.TranslationKey)
{
UpdateContentItem(childTarget, childSource);
childTarget.SortOrder = childSource.SortOrder;
bDelete = false;
}
}
if (bDelete)
{
_persister.Delete(childTarget);
//N2.Context.Current.Resolve<N2.Edit.Trash.ITrashHandler>().Purge(childTarget);
}
else
bDelete = true;
}
keysTarget = new List<string>(target.GetChildren(new Collections.PartFilter()).Keys);
foreach (string keySource in keysSource)
{
ContentItem childSource = source.Children[keySource];
foreach (string keyTarget in keysTarget)
{
ContentItem childTarget = target.Children[keyTarget];
if (childSource.TranslationKey == childTarget.TranslationKey)
{
bAdd = false;
}
}
if (bAdd)
{
childSource.ID = 0;
childSource.Parent = null;
toCopy.Add(source.Children.IndexOf(childSource), childSource);
}
else
bAdd = true;
}
ContentItem copyChild;
foreach (int key in toCopy.Keys)
{
if (toCopy.TryGetValue(key, out copyChild))
{
copyChild.InsertAt(target, key);
}
}
}
示例3: ConvertCurrency
public string ConvertCurrency(HttpContext context)
{
string errorCode = string.Empty;
string res = string.Empty;
string SourceCountryName = string.Empty;
string TargetCountryName = string.Empty;
string InputAmt = string.Empty;
string OutputAmt = string.Empty;
string jsonRes = string.Empty;
string ConversionResult = string.Empty;
Dictionary<string, string> values = new System.Collections.Generic.Dictionary<string, string>();
// string RawUrlx = "http://localhost/currency/?from=USD&to=EUR&q=1";
string RawUrlx = context.Request.RawUrl;
NameValueCollection nm = HttpUtility.ParseQueryString(RawUrlx.Substring(RawUrlx.IndexOf("/?") + 2));
// Populating the key-value object just for validation purposes
SourceCountryName = nm.Get("from");
TargetCountryName = nm.Get("to");
InputAmt = nm.Get("q");
nm.Add("ConvertedAmt", string.Empty);
//Do All the good stuff here - validation
// Here I could rather copy the request url and use it for the API post
var requestUri = string.Format("http://rate-exchange.appspot.com/currency?from={0}&to={1}&q={2}", Uri.EscapeDataString(SourceCountryName), Uri.EscapeDataString(TargetCountryName), Uri.EscapeDataString(InputAmt));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
// Reading the API call result, parsing the JSON object and populating the key-value object
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
jsonRes = reader.ReadToEnd();
values = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonRes);
}
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
}
throw;
}
//Validating Conversion API result
values.TryGetValue("err", out errorCode);
values.TryGetValue("warning", out errorCode);
if (!String.IsNullOrEmpty(errorCode))
return errorCode;
values.TryGetValue("v", out OutputAmt);
return OutputAmt;
}