对象表达式(F#)
http://msdn.microsoft.com/en-us/library/dd233237.aspx
定义:
An object expression is an expression that creates a new instance of a@H_403_10@ dynamically created,anonymous object type that is based on an@H_403_10@ existing base type,interface,or set of interfaces.
我想知道,C#中是否存在类似的东西?@H_403_10@或者这些东西只能用F#编程语言?
解决方法
匿名类型无法符合接口或从任何东西派生.
Lambdas和delegates提供函数作为变量.
Object Expressions的文档声明了这一点,这使它听起来像语言方便.
You use object expressions when you want to avoid the extra code and@H_403_10@ overhead that is required to create a new,named type. If you use@H_403_10@ object expressions to minimize the number of types created in a@H_403_10@ program,you can reduce the number of lines of code and prevent the@H_403_10@ unnecessary proliferation of types. Instead of creating many types@H_403_10@ just to handle specific situations,you can use an object expression@H_403_10@ that customizes an existing type or provides an appropriate@H_403_10@ implementation of an interface for the specific case at hand.
你可以用这样的东西伪造它(虽然你不能将它限制在一个接口)
var foo = new { square = new Func<int,int>(x => x * x) };