0

I have a variable of type dynamic in my code what I am trying to do is get the type of the assigned object but it seems that there are no properties or methods available in the dynamic field.

My code is something like:

dynamic readings;

private void method()
{
    Type type= readings.GetType();
}

Am I doing something wrong here?

Reference for using GetType:

How do I check type of dynamic datatype at runtime?

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • What is the value of your `readings` field when you call `method()`? – canton7 Oct 23 '19 at 11:44
  • 4
    Have you tried the accepted answer from the question linked in your post? https://stackoverflow.com/a/23143276/5062791 – ColinM Oct 23 '19 at 11:46
  • Your stated reference does not directly use `GetType()` but instead `((ObjectHandle)readings).Unwrap().GetType()` – user1781290 Oct 23 '19 at 11:47
  • Additionally, from Lorenzo's answer in that same post, if the value is null then `type = readings == null ? null : readings.GetType();` – ColinM Oct 23 '19 at 11:50
  • Well the thing is i cannot find `ObjectHandle` in my xamarin project – FreakyAli Oct 23 '19 at 11:52
  • It's available in Xamarin, you need to add reference to System.Runtime https://learn.microsoft.com/en-us/dotnet/api/system.runtime.remoting.objecthandle.unwrap?view=netframework-4.8 – ColinM Oct 23 '19 at 11:55
  • I have already tried the accepted answer, ObjectHandle is not available in Xamarin.Forms .Net Standard 2.0! – FreakyAli Oct 23 '19 at 11:55
  • Lemme try that quickly – FreakyAli Oct 23 '19 at 11:55
  • 2
    Every time you use `dynamic`, a kitten dies... – DavidG Oct 23 '19 at 11:58
  • Most of the time you use `dynamic` you´re using it whrong. Having said this, why not just use `object` in the first place, instead of `dynamic`? – MakePeaceGreatAgain Oct 23 '19 at 12:05
  • @ColinM Well `System.Runtime` does not seem to have ObjectHandle for some reason! – FreakyAli Oct 23 '19 at 12:05
  • @DavidG Tell that to my API Developer... – FreakyAli Oct 23 '19 at 12:05
  • @HimBromBeere I am not a big fan of dynamic either, to be honest, but what can I do here I am not the one in control... – FreakyAli Oct 23 '19 at 12:07
  • But this is your code, so why can't you stop using dynamic? – DavidG Oct 23 '19 at 12:07
  • I already did actually, Thanks for the suggestion anyway. After reading about it I realised. it does not fit my requirement anyway. `Object` it is :D – FreakyAli Oct 23 '19 at 12:08
  • Well, I would also say using `object` is a code smell too, but since we cannot see your code, I cannot suggest anything better. – DavidG Oct 23 '19 at 12:11
  • Well, the thing is that we do not have a proper response, based on the case the response can be of more than 5 types hence this... – FreakyAli Oct 23 '19 at 12:32
  • If you know the response, then you should have 5 types. – DavidG Oct 23 '19 at 12:39
  • Umm, how can I have five types of one property? Also, more types can be added in the future :/ – FreakyAli Oct 23 '19 at 12:40
  • 1
    But if you don't know what is in the object, how can you possibly use it? – DavidG Oct 23 '19 at 12:47
  • "How can I have five types of one property" - Welcome to Generics, https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/ – ColinM Oct 23 '19 at 13:14
  • Again I am aware of generics but you are misunderstanding my question. I have to deserialize this object from an API and then check what I got, using generics? – FreakyAli Oct 23 '19 at 13:52

1 Answers1

4

just cast it to object:

Type type = ((object)readings).GetType();

Being dynamic means that all calls can be intercepted, but that's a compiler trick, not an inherent feature of the type. Casting it to object means that the compiler stops doing that. Behind the scenes, dynamic is just a fancy word for object anyway.

Note, however, that it is usually a bad idea to mix reflection (GetType()) and dynamic; while objects can work as dynamic (by re-exposing the reflection API as dynamic), this is not always the case, and many (most?) implementations of dynamic are presenting entirely artificial members that do not exist in terms of reflection. That's kinda the main point of dynamic, with "oh, it also lets you be lazy and talk to types without knowing their type" just a handy side-effect.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900