简体   繁体   English

具有ExpandoObject的dynamic关键字,用于附加和删除属性

[英]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. 我知道现在在C#3.0中,我们可以在运行时使用dynamic关键字添加/删除属性,例如将Object作为通用容器的JavaScript概念,但我有一个问题。

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: 我将从Person类创建一个新对象:

        dynamic p = new ExpandoObject();

Now is this object really an object of class Person ? 现在这个对象真的是Person类的对象吗? it dose not related to Person class anymore ?? 它不再与Person类无关?

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

Now i will set values for properties and Expand object p with new property 'Foo': 现在,我将为属性设置值,并使用新属性'Foo'扩展对象p:

        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: 我重视的新属性“富”和财产“年龄”的设定值,但我做了类型的对象System.Dynamic.ExpandoObject不类型的Person ,所以我在这部分有不匹配的,是我真的扩大从类型对象我需要,我可以使用强制转换来引用这样的对象类型吗?

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

An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll System.Core.dll中发生了类型为“ Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”的未处理异常

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. 通常很难知道C# 2之间的区别,因为它们都使用相同的C#运算符:强制转换。

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 . 最好的API是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. 如果您愿意,我可以编写ConvertTo<T>声明并在此处进行解释。

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... 我不确定您为什么要这么做...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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