本文整理汇总了C#中JToken.Cast方法的典型用法代码示例。如果您正苦于以下问题:C# JToken.Cast方法的具体用法?C# JToken.Cast怎么用?C# JToken.Cast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JToken
的用法示例。
在下文中一共展示了JToken.Cast方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PopulateDynamicItem
public static dynamic PopulateDynamicItem(Type targetType, JToken actualItem, dynamic dynamicItem)
{
var propDict = actualItem.Cast<JProperty>().ToDictionary(p => p.Name, p => p.Value);
foreach (var property in targetType.GetProperties().Where(p => p.SetMethod != null))
{
if (propDict.ContainsKey(property.Name))
{
JToken token = propDict[property.Name];
Type type = GetType(token);
if (type != typeof(DateTimeOffset))
{
var value = Convert.ChangeType(token.ToString(), type);
property.SetValue(dynamicItem, value);
}
}
else if (propDict.ContainsKey("RegistrationStub." + property.Name))
{
JToken token = propDict["RegistrationStub." + property.Name];
Type type = GetType(token);
if (type != typeof(DateTimeOffset))
{
var value = Convert.ChangeType(token.ToString(), type);
property.SetValue(dynamicItem, value);
}
}
}
dynamicItem.PartitionKey = propDict["Event.EventKey"].ToString();
dynamicItem.RowKey = Guid.NewGuid().ToString();
return dynamicItem;
}
示例2: GenerateDynamicItem
public static dynamic GenerateDynamicItem(JToken actualItem)
{
var propDict = actualItem.Cast<JProperty>().Where(p => p.Name.StartsWith("RegistrationStub.")).ToDictionary(p => p.Name.Replace("RegistrationStub.", String.Empty), p => p.Value);
IEnumerable<PropertySpec> types = propDict.Select(p => new PropertySpec(GetType(p.Value), p.Key));
Type targetType = DynamicTypeFactory.GenerateDynamicType(TargetType, true, types.ToArray<PropertySpec>());
dynamic dynamicItem = DynamicTypeFactory.GenerateCustomItem(targetType);
return PopulateDynamicItem(targetType, actualItem, dynamicItem);
}
示例3: AssertPropertyValuesMatch
private bool AssertPropertyValuesMatch(JToken httpBody1, JToken httpBody2)
{
switch (httpBody1.Type)
{
case JTokenType.Array:
{
if (httpBody1.Count() != httpBody2.Count())
{
_reporter.ReportError(expected: httpBody1.Root, actual: httpBody2.Root);
return false;
}
for (var i = 0; i < httpBody1.Count(); i++)
{
if (httpBody2.Count() > i)
{
var isMatch = AssertPropertyValuesMatch(httpBody1[i], httpBody2[i]);
if (!isMatch)
{
break;
}
}
}
break;
}
case JTokenType.Object:
{
foreach (JProperty item1 in httpBody1)
{
var item2 = httpBody2.Cast<JProperty>().SingleOrDefault(x => x.Name == item1.Name);
if (item2 != null)
{
var isMatch = AssertPropertyValuesMatch(item1, item2);
if (!isMatch)
{
break;
}
}
else
{
_reporter.ReportError(expected: httpBody1.Root, actual: httpBody2.Root);
return false;
}
}
break;
}
case JTokenType.Property:
{
var httpBody2Item = httpBody2.SingleOrDefault();
var httpBody1Item = httpBody1.SingleOrDefault();
if (httpBody2Item == null && httpBody1Item == null)
{
return true;
}
if (httpBody2Item != null && httpBody1Item != null)
{
AssertPropertyValuesMatch(httpBody1Item, httpBody2Item);
}
else
{
_reporter.ReportError(expected: httpBody1.Root, actual: httpBody2.Root);
return false;
}
break;
}
case JTokenType.Integer:
case JTokenType.String:
{
if (!httpBody1.Equals(httpBody2))
{
_reporter.ReportError(expected: httpBody1.Root, actual: httpBody2.Root);
return false;
}
break;
}
default:
{
if (!JToken.DeepEquals(httpBody1, httpBody2))
{
_reporter.ReportError(expected: httpBody1.Root, actual: httpBody2.Root);
return false;
}
break;
}
}
return true;
}