如何在运行时检查动态数据类型的类型? [英] How do I check type of dynamic datatype at runtime?

查看:105
本文介绍了如何在运行时检查动态数据类型的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET网站中,我有一个方法返回类型为 dynamic 的值。然后,根据某些条件和结果,此方法将返回布尔值或SortedList。

In my ASP.NET website, I have a method that returns a value of type dynamic. This method, depending on certain criteria and results, will then either return a Boolean value or SortedList.

要粘贴的代码太多,但例如:

There is too much code to paste, but for example:

public dynamic ReturnThis(dynamic value)
{
    if(someConditionIsMet)
    {
        value = true;
    }
    else
    {
        value = new List<String>().Add(new Person() { Name = "Travis" });
    }

    return value;
}

我的问题是,我想确定数据类型的值,在调用此方法之后才对其进行操作或读取其数据。但是我不确定如何检查动态值是什么类型。我该怎么做?

My problem is, I would like to determine the datatype of value after calling this method before acting on or reading its data. But I am unsure how to check what type dynamic value is. How can I do this?

推荐答案

只需在另一个SO问题上阅读此文件……希望它将为您解决问题:

Just read this on another SO question...hopefully it will do the trick for you:

Type unknown = ((ObjectHandle)tmp).Unwrap().GetType();

阅读并支持此问题以获取更多信息:
获取声明为动态的对象的类型

Read and upvote this question for more info: get the Type for a object declared dynamic

这篇关于如何在运行时检查动态数据类型的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆