0

我不想创建一个新类并决定使用匿名类型来将数据从表示层发送到业务层。

但我的问题是如何在 biz 层中引入匿名类型?我使用 Vb.Net 2008 和 VS 2008。

编辑

实际上,我必须处理一些数据,它们的性质实际上是临时类,我不会在我自己的项目中再次使用它们。

4

3 回答 3

2

Anonymous types are scoped so that they can be accessed in a strongly typed way only inside the method in which they were created. If you want to pass them from method to another, you'll have to resort to passing them as object and using reflection (or dynamic) to access their properties, which most certainly isn't what you want.

Use a proper class for your data.

于 2011-01-31T13:29:07.740 回答
0

I don't recomment you make such things. Such code won't be maintainable. But you could use dynamic from 4.0, that is no good for this.

于 2011-01-31T13:29:22.230 回答
0

不要,不要!

如果他们生活在服务器边界上,您最终会dynamic在您的功能的客户端上使用,这是不好的。

这是因为您可能必须像object在方法签名中那样公开对象,并且您将失去类型安全的所有好处。

另一方面,你什么也没做,编译器无论如何都会为你吐出一个类。

于 2011-01-31T13:27:17.393 回答