本文整理汇总了C#中JObject.ToObject方法的典型用法代码示例。如果您正苦于以下问题:C# JObject.ToObject方法的具体用法?C# JObject.ToObject怎么用?C# JObject.ToObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JObject
的用法示例。
在下文中一共展示了JObject.ToObject方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
}
示例2: Live
public IHttpActionResult Live(JObject value) {
var authInfo = value.ToObject<ExternalAuthInfo>();
if (authInfo == null || String.IsNullOrEmpty(authInfo.Code))
return NotFound();
if (String.IsNullOrEmpty(Settings.Current.MicrosoftAppId) || String.IsNullOrEmpty(Settings.Current.MicrosoftAppSecret))
return NotFound();
var client = new WindowsLiveClient(new RequestFactory(), new RuntimeClientConfiguration {
ClientId = Settings.Current.MicrosoftAppId,
ClientSecret = Settings.Current.MicrosoftAppSecret,
RedirectUri = authInfo.RedirectUri
});
UserInfo userInfo;
try {
userInfo = client.GetUserInfo(authInfo.Code);
} catch (Exception ex) {
Log.Error().Exception(ex).Message("Unable to get user info.").Write();
return BadRequest("Unable to get user info.");
}
User user;
try {
user = AddExternalLogin(userInfo);
} catch (Exception ex) {
Log.Error().Exception(ex).Message("Unable to get user info.").Write();
return BadRequest("An error occurred while processing user info.");
}
if (user == null)
return BadRequest("Unable to process user info.");
if (!String.IsNullOrEmpty(authInfo.InviteToken))
AddInvitedUserToOrganization(authInfo.InviteToken, user);
return Ok(new { Token = GetToken(user) });
}