Tag: 匿名类

如何将匿名类型转换为已知类型

我有一个匿名类型变量。 这个变量来自另一个函数,我们无法改变它。 // var a {property1 = “abc”; property2 = “def”} 我上课了: class Myclass{ string property1; string property2; } 如何将变量a转换为Myclass类型。 我试过了 Myclass b = (Myclass)a; 但它不起作用。 如果我初始化: Myclass b = new Myclass{ property1 = a.property1, property2 = a.property2, } 它工作正常,但需要大量代码,因为MyClass有很多属性 谁能帮我? 谢谢你的回答。