Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 543
uSeRnAmEhAhAhAhAhA Avatar asked Apr 17 '14 20:04

uSeRnAmEhAhAhAhAhA


People also ask

What is the dynamic data type?

The IDL language is dynamically typed. This means that an operation on a variable can change that variable's type. In general, when variables of different types are combined in an expression, the result has the data type that yields the highest precision.

What are dynamic types in C#?

In C# 4.0, a new type is introduced that is known as a dynamic type. It is used to avoid the compile-time type checking. The compiler does not check the type of the dynamic type variable at compile time, instead of this, the compiler gets the type at the run time.

Which of the following is true for dynamic type in C sharp?

When an object type is converted to a value type, it is called unboxing. Q 17 - Which of the following is correct about dynamic Type in C#? A - You can store any type of value in the dynamic data type variable. B - Type checking for these types of variables takes place at run-time.


4 Answers

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();         }     } } 
like image 154
uSeRnAmEhAhAhAhAhA Avatar answered Sep 22 '22 06:09

uSeRnAmEhAhAhAhAhA


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

like image 29
Troy Carlson Avatar answered Sep 21 '22 06:09

Troy Carlson


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()

like image 38
Smeegs Avatar answered Sep 19 '22 06:09

Smeegs


Given a dynamic type:

dynamic dynVar;
Type type; 

A merely declared, uninitialized dynamic variable dynVar will throw an exception of Type Microsoft.CSharp.RuntimeBinder.RuntimeBinderException as you are performing runtime binding on a null reference, when performing Type-Reflection via dynVar.GetType().

  • As pointed out by "Troy Carlson", one may use, the rather slow method via a remoted MarshalByRefObject:

     Type type = ((ObjectHandle)dynVar).Unwrap().GetType();
     // > type...is null
    
  • But a simple null check as for any other type would suffice:

     type = dynVar == null ? null : dynVar.GetType();
    
  • or...

     type = dynVar?.GetType();
    
like image 26
Lorenz Lo Sauer Avatar answered Sep 20 '22 06:09

Lorenz Lo Sauer