简体   繁体   中英

dynamic keyword with ExpandoObject to attach and remove properties

I know that now in C# 3.0 we can use dynamic keyword to add/remove properties in run time like the Object as general container's JavaScript concept, but i have a question.

Is it really Expand the object of the type i want ??

For Example: We have class:

    class Person
{
    public string Name { get; set; }
    public int Age { get; set;}
}

I will create a new object from Person Class:

        dynamic p = new ExpandoObject();

Now is this object really an object of class Person ? it dose not related to Person class anymore ??

Console.WriteLine(p.GetType()); // System.Dynamic.ExpandoObject

Now i will set values for properties and Expand object p with new property 'Foo':

        p.Age = 25;
        p.Foo = "foo"; 

I attached new property 'Foo' and set value for property 'Age' but i do that for an object of type System.Dynamic.ExpandoObject not of type Person so i have mismatch in this part, is i really expand the object from the type i needed, can i uses cast to refer to the object type like that:

Console.WriteLine(((Person)p).Name);  

An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll

I think you're confusing the issues of casting and converting here.

  • Casting : The act of changing the type of a reference which points to an object. Either moving up or down the object hierarchy or to an implemented interface

  • Converting : Creating a new object from the original source object of a different type and accessing it through a reference to that type.

It's often hard to know the difference between the 2 in C# because both of them use the same C# operator: the cast.

In this situation you are almost certainly not looking for a cast operation. Casting a dynamic to another dynamic is essentially an identity conversion. It provides no value because you're just getting a dynamic reference back to the same underlying object. The resulting look up would be no different.

Instead what you appear to want in this scenario is a conversion. That is morphing the underlying object to a different type and accessing the resulting object in a dynamic fashion. The best API for this is Convert.ChangeType .

public static dynamic Convert(dynamic source, Type dest) {
  return Convert.ChangeType(source, dest);
}

In this case, instead of :

Console.WriteLine(((Person)p).Name);  

try :

Console.WriteLine(ConvertTo<Person>(p).Name);  

If you want i may write ConvertTo<T> declaration and explain it here.

You can't cast from a dynamic type to a solid type like this as the type doesn't have anything to do with dynamic at that time other than looking like it as it has similar properties.

However, you could serialize the dynamic instance and then deserialize it to the solid type that you have. I'm not sure why you would want to do that though...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM