Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast dynamic to specific type

Tags:

c#

dynamic

I am retrieving JSON with different object type in one part. I made this part as dynamic.

I need to get data from this object so I created class which looks the same as the dynamic data like below:

public class SpecificObject1
{
    public string Title{get; set;}
    public object[] ViewElements{get; set}
    public object AnyAttributes{get; set;}
}

Here is how I want to convert it to this specific object:

  var @switch = new Dictionary<Type, Action> {
                { typeof(x), () =>  jsonObj.Some = jsonObj.Some as SpecificObject1},
                { typeof(y), () =>  ......}
            };

Casting with as returns null.

EDIT: Changed properties to real one

dynamic contains:

AnyAttributes: {object}
Title: "title"
ViewElements: object[0]
like image 633
miechooy Avatar asked Jun 28 '16 06:06

miechooy


People also ask

How do you cast dynamic to INT in darts?

A string can be cast to an integer using the int. parse() method in Dart. The method takes a string as an argument and converts it into an integer.

How do you convert dynamic to string in darts?

In dart and flutter, this example converts a list of dynamic types to a list of Strings. map() is used to iterate over a list of dynamic strings. To convert each element in the map to a String, toString() is used. Finally, use the toList() method to return a list.

Is it good to use dynamic type in C#?

The dynamic type has been added to C# since version 4 as because of the need to improve interoperability with COM (Component Object Model) and other dynamic languages. While that can be achieved with reflection, dynamic provides a natural and more intuitive way to implement the same code.

What is the dynamic type C#?

The dynamic type is a static type, but an object of type dynamic bypasses static type checking. In most cases, it functions like it has type object . At compile time, an element that is typed as dynamic is assumed to support any operation.


2 Answers

You can´t change the type of an object - be it dynamic or any other compile-time type. Thus even if you assign jsonObj.Some as SpecificObject1 to jsonObj.Some you can´t change its compiletime type (probably dynamic in your case which is a compiletime-type).

This would imply you could to this:

int a = 3;
a = a as string;

Which is obvious non-sense. a is of type int which can´t be changed. So even if you *could cast a to a string you can´t assign the result (which would be of type sting) to a because a actually is of type int.

The same applies to an instance of dynamic:

dynamic b = a as Bar;

This will still evaluate to b being of type dynamic. However if a was a Bar-instance before, the runtime-type of b surely is Bar as well. Anyway you don´t get anything by this cast as the compile-time-type of b is still dynamic - making it a no-op.

EDIT: In order to get a compile-time type which you can use you have to create a new instance of SpecificType based on jsonObj.Some:

var newValue = new SpecificObject {
    Title = jsonObj.Some.Title,
    ViewElements = jsonObj.Some.ViewElements,
    AnyAttributes = jsonObj.Some.AnyAttributes 
}

However you can´t assign this to jsonObj.Some and expect the latter to be of type SpecificObject at compile-time. Anyway as it already is dynamic you can do everything you want with it, for instance set its Title:

jsonObj.Some.Title = "NewTitle";

You won´t need any cast for this.

like image 78
MakePeaceGreatAgain Avatar answered Sep 30 '22 19:09

MakePeaceGreatAgain


You can use a Slapper.AutoMapper functionality. Use MapDynamic() method for your needs.

public class Person
{
    public int Id;
    public string FirstName;
    public string LastName;
}

[Test]
public void Can_Map_Matching_Field_Names_Using_Dynamic()
{
    // Arrange
    dynamic dynamicPerson = new ExpandoObject();
    dynamicPerson.Id = 1;
    dynamicPerson.FirstName = "Clark";
    dynamicPerson.LastName = "Kent";

    // Act
    var person = Slapper.AutoMapper.MapDynamic<Person>( dynamicPerson ) as Person;

    // Assert
    Assert.NotNull( person );
    Assert.That( person.Id == 1 );
    Assert.That( person.FirstName == "Clark" );
    Assert.That( person.LastName == "Kent" );
}
like image 35
trueboroda Avatar answered Sep 30 '22 19:09

trueboroda