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]
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.
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.
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.
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.
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.
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" );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With