How do I check type of dynamic datatype at runtime? How do I check type of dynamic datatype at runtime? asp.net asp.net

How do I check type of dynamic datatype at runtime?


Both solutions are working for me. In the documentation Smeegs linked to, the is keyword was mentioned. And I came up with a slightly more readable solution:

if(value is Boolean) { } and if(value is List<Person>) { }


A working test:

using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication3348{    class Program    {        class Person        {            string Name = "";        }        static void Main(string[] args)        {            Console.WriteLine("Assigning List to value");            dynamic value = new List<Person>();            if (value is List<Person>)            {                Console.WriteLine("value is a list");            }            value = false;            Console.WriteLine("Assigning bool to value");            if (value is Boolean)            {                Console.WriteLine("value is bool");            }            Console.Read();        }    }}


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


You should just be able to use GetType(). Like so:

dynamic returnedValue = ReturnThis(value);var returnType = returnedValue.GetType();

Here is some more information on GetType()