本文整理汇总了C#中JObject.TryGetValue方法的典型用法代码示例。如果您正苦于以下问题:C# JObject.TryGetValue方法的具体用法?C# JObject.TryGetValue怎么用?C# JObject.TryGetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JObject
的用法示例。
在下文中一共展示了JObject.TryGetValue方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetTypeFromJObject
private IElasticType GetTypeFromJObject(JObject po, JsonSerializer serializer)
{
JToken typeToken;
JToken propertiesToken;
JToken fieldsToken;
var type = string.Empty;
var hasType = po.TryGetValue("type", out typeToken);
if (hasType)
type = typeToken.Value<string>().ToLowerInvariant();
else if (po.TryGetValue("properties", out propertiesToken))
type = "object";
var originalType = type;
var hasFields = po.TryGetValue("fields", out fieldsToken);
if (hasFields)
type = "multi_field";
serializer.TypeNameHandling = TypeNameHandling.None;
switch (type)
{
case "string":
return serializer.Deserialize(po.CreateReader(), typeof(StringMapping)) as StringMapping;
case "float":
case "double":
case "byte":
case "short":
case "integer":
case "long":
return serializer.Deserialize(po.CreateReader(), typeof(NumberMapping)) as NumberMapping;
case "date":
return serializer.Deserialize(po.CreateReader(), typeof(DateMapping)) as DateMapping;
case "boolean":
return serializer.Deserialize(po.CreateReader(), typeof(BooleanMapping)) as BooleanMapping;
case "binary":
return serializer.Deserialize(po.CreateReader(), typeof(BinaryMapping)) as BinaryMapping;
case "object":
return serializer.Deserialize(po.CreateReader(), typeof(ObjectMapping)) as ObjectMapping;
case "nested":
return serializer.Deserialize(po.CreateReader(), typeof(NestedObjectMapping)) as NestedObjectMapping;
case "multi_field":
var m =serializer.Deserialize(po.CreateReader(), typeof(MultiFieldMapping)) as MultiFieldMapping;
m.Type = originalType;
return m;
case "ip":
return serializer.Deserialize(po.CreateReader(), typeof(IPMapping)) as IPMapping;
case "geo_point":
return serializer.Deserialize(po.CreateReader(), typeof(GeoPointMapping)) as GeoPointMapping;
case "geo_shape":
return serializer.Deserialize(po.CreateReader(), typeof(GeoShapeMapping)) as GeoShapeMapping;
case "attachment":
return serializer.Deserialize(po.CreateReader(), typeof(AttachmentMapping)) as AttachmentMapping;
}
return null;
}
示例2: TryGetValue
public void TryGetValue()
{
JObject o = new JObject();
o.Add("PropertyNameValue", new JValue(1));
Assert.AreEqual(1, o.Children().Count());
JToken t;
Assert.AreEqual(false, o.TryGetValue("sdf", out t));
Assert.AreEqual(null, t);
Assert.AreEqual(false, o.TryGetValue(null, out t));
Assert.AreEqual(null, t);
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
Assert.AreEqual(true, JToken.DeepEquals(new JValue(1), t));
}
示例3: GetTypeFromJObject
private IElasticCoreType GetTypeFromJObject(JObject po, JsonSerializer serializer)
{
JToken typeToken;
serializer.TypeNameHandling = TypeNameHandling.None;
if (po.TryGetValue("type", out typeToken))
{
var type = typeToken.Value<string>().ToLowerInvariant();
switch (type)
{
case "string":
return serializer.Deserialize(po.CreateReader(), typeof(StringMapping)) as StringMapping;
case "float":
case "double":
case "byte":
case "short":
case "integer":
case "long":
return serializer.Deserialize(po.CreateReader(), typeof(NumberMapping)) as NumberMapping;
case "date":
return serializer.Deserialize(po.CreateReader(), typeof(DateMapping)) as DateMapping;
case "boolean":
return serializer.Deserialize(po.CreateReader(), typeof(BooleanMapping)) as BooleanMapping;
case "binary":
return serializer.Deserialize(po.CreateReader(), typeof(BinaryMapping)) as BinaryMapping;
}
}
return null;
}
示例4: DictionaryItemShouldSet
public void DictionaryItemShouldSet()
{
JObject o = new JObject();
o["PropertyNameValue"] = new JValue(1);
Assert.AreEqual(1, o.Children().Count());
JToken t;
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
Assert.AreEqual(true, JToken.DeepEquals(new JValue(1), t));
o["PropertyNameValue"] = new JValue(2);
Assert.AreEqual(1, o.Children().Count());
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
Assert.AreEqual(true, JToken.DeepEquals(new JValue(2), t));
o["PropertyNameValue"] = null;
Assert.AreEqual(1, o.Children().Count());
Assert.AreEqual(true, o.TryGetValue("PropertyNameValue", out t));
Assert.AreEqual(true, JToken.DeepEquals(new JValue((object)null), t));
}
示例5: Invoke
public static object Invoke(MethodInfo methodInfo, JObject json)
{
ParameterInfo[] paramterInfos = methodInfo.GetParameters();
var type = methodInfo.DeclaringType;
object[] paramters = new object[paramterInfos.Length];
try
{
for (int i = 0; i < paramterInfos.Length; i++)
{
Type parameterType = paramterInfos[i].ParameterType;
string parameterName = paramterInfos[i].Name;
object value = null;
JToken jvalue = null;
if (json.TryGetValue(parameterName, StringComparison.OrdinalIgnoreCase, out jvalue))
{
if (parameterType == typeof(string))
value = jvalue.ToString();
else
value = jvalue.ToObject(parameterType);
}
else
{
if (parameterType == typeof(string))
value = json.ToString();
else
value = json.ToObject(parameterType);
}
paramters[i] = value;
}
}
catch (Exception ex)
{
throw new Exception("解析方法'" + type.FullName + "." + methodInfo.Name + "'参数出错,请检查传入参数!\n出错信息:" + ex.Message, ex);
}
try
{
object instance = null;
if (!methodInfo.IsStatic)
instance = Activator.CreateInstance(type, new object[] { });
return methodInfo.Invoke(instance, paramters);
}
catch (Exception ex)
{
throw new Exception("调用方法'" + type.FullName + "." + methodInfo.Name + "'失败\n出错信息:" + ex.Message, ex);
}
}
示例6: GetValue
public void GetValue()
{
JObject a = new JObject();
a["Name"] = "Name!";
a["name"] = "name!";
a["title"] = "Title!";
Assert.Equal(null, a.GetValue("NAME", StringComparison.Ordinal));
Assert.Equal(null, a.GetValue("NAME"));
Assert.Equal(null, a.GetValue("TITLE"));
Assert.Equal("Name!", (string)a.GetValue("NAME", StringComparison.OrdinalIgnoreCase));
Assert.Equal("name!", (string)a.GetValue("name", StringComparison.Ordinal));
Assert.Equal(null, a.GetValue(null, StringComparison.Ordinal));
Assert.Equal(null, a.GetValue(null));
JToken v;
Assert.False(a.TryGetValue("NAME", StringComparison.Ordinal, out v));
Assert.Equal(null, v);
Assert.False(a.TryGetValue("NAME", out v));
Assert.False(a.TryGetValue("TITLE", out v));
Assert.True(a.TryGetValue("NAME", StringComparison.OrdinalIgnoreCase, out v));
Assert.Equal("Name!", (string)v);
Assert.True(a.TryGetValue("name", StringComparison.Ordinal, out v));
Assert.Equal("name!", (string)v);
Assert.False(a.TryGetValue(null, StringComparison.Ordinal, out v));
}