Search code examples
c#asp.net.netdynamictypes

How do I check type of dynamic datatype at runtime?


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?


Solution

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

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

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