本文整理汇总了C#中Key.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Key.GetType方法的具体用法?C# Key.GetType怎么用?C# Key.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Key
的用法示例。
在下文中一共展示了Key.GetType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseLine
/// <summary>
/// Parses a line from the csv file and constructs a key from it.
/// </summary>
/// <param name="line">The line to be parsed.</param>
/// <param name="languages">The languages</param>
/// <param name="fillMissing">Whether to fill the missing values</param>
/// <returns>The key constructed from the line of csv. Null if line is irrelevant.</returns>
public static Key ParseLine(string line, List<string> languages, bool fillMissing = false)
{
// return null if line is invalid or irrelevant
if (string.IsNullOrWhiteSpace(line) || line.Trim().StartsWith("//") || line.Trim().StartsWith("\"//") ||
line.Contains("LANGUAGE,"))
{
return null;
}
// replace all whitespace with spaces
line = Regex.Replace(line, @"\s+", " ");
// split the string by commas taking into account that commas might be in quotes
var resultArray = Regex.Split(line.Trim(), ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
// the first value becomes the key
var result = new Key
{
Id = resultArray[0]
};
// if mismatch in number of values do nothing with the extra
// if (resultArray.GetLength(0) - 1 != languages.Count) throw new ApplicationException(string.Format("The provided line contains more values than specified languages: {0}", line));
// add the languages and the values to the dictionary
for (int i = 1; i <= languages.Count; i++)
{
// trim
resultArray[i] = resultArray[i].Trim();
// remove unnecessary quotes
if (resultArray[i].StartsWith("\"") && resultArray[i].EndsWith("\""))
{
resultArray[i] = resultArray[i].Substring(1, resultArray[i].Length - (1 * 2));
}
try
{
result.GetType().GetProperty(languages[i - 1]).SetValue(result, resultArray[i]);
}
catch (Exception ex)
{
throw new ApplicationException(string.Format("Could not set key value. Lang: {1}, Val: {2}. Error: {0}", ex.Message, languages[i - 1], resultArray[i]));
}
}
result.FillOriginalKeyWithEnglish();
// fill the languages that were not found
if (fillMissing)
{
result.FillEmptyKeysWithEnglishOrOriginal();
}
return result;
}