简体   繁体   English

C#中的“对象表达”是否存在于F#中?

[英]Does “Object Expression” exist in C# like in F#?

There is one interesting concept in F# language: F#语言中有一个有趣的概念:

Object Expressions (F#)

http://msdn.microsoft.com/en-us/library/dd233237.aspx http://msdn.microsoft.com/en-us/library/dd233237.aspx

Definition: 定义:

An object expression is an expression that creates a new instance of a dynamically created, anonymous object type that is based on an existing base type, interface, or set of interfaces. 对象表达式是一种表达式,它创建基于现有基本类型,接口或接口集的动态创建的匿名对象类型的新实例。

I wonder, does something similar exist in C#? 我想知道,C#中是否存在类似的东西? Or this stuff is only available in F# programming language? 或者这些东西只能用F#编程语言?

No that does not exist. 不,那不存在。 I think the closest thing is a class that implements those interfaces. 我认为最接近的是实现这些接口的class

Anonymous types are unable to conform to an interface or derive from anything. 匿名类型无法符合接口或从任何东西派生。

Lambdas and delegates offer functions as variables. Lambdas和delegates提供函数作为变量。

The documentation for Object Expressions state this which makes it sound like its a language convenience. Object Expressions的文档声明了这一点,这使它听起来像语言方便。

You use object expressions when you want to avoid the extra code and overhead that is required to create a new, named type. 如果要避免创建新的命名类型所需的额外代码和开销,可以使用对象表达式。 If you use object expressions to minimize the number of types created in a program, you can reduce the number of lines of code and prevent the unnecessary proliferation of types. 如果使用对象表达式来最小化程序中创建的类型数,则可以减少代码行数并防止不必要的类型扩散。 Instead of creating many types just to handle specific situations, you can use an object expression that customizes an existing type or provides an appropriate implementation of an interface for the specific case at hand. 您可以使用自定义现有类型的对象表达式,或者为手头的特定案例提供适当的接口实现,而不是仅仅为了处理特定情况而创建许多类型。

You could fake it (although you couldn't constrain it to an interface) with something like this 你可以用这样的东西伪造它(虽然你不能将它限制在一个接口)

var foo = new { square = new Func<int, int>(x => x * x) };

A pattern that I use is to create a bunch of "ad-hoc" classes that implement an interface that I instantiate with a delegate (usually a closure): 我使用的模式是创建一堆“ad-hoc”类,它们实现了一个我用委托实例化的接口(通常是一个闭包):

For instance: 例如:

public sealed class AdHocEquatable<T> : IEquatable<T>
{
    readonly Func<T, bool> equals;

    public AdHocEquatable(Func<T, bool> eq)
    {
        equals = eq;
    }

    public bool Equals(T other)
    {
        return equals(other);
    }
}

Then use like so: 然后使用如下:

var eq = new AdHocEquatable<Person>(p => p.ID == some_person_ref.ID);

In my experience, object expressions are needed for only a handful of interfaces (usually from the the BCL). 根据我的经验,只有少数几个接口(通常来自BCL)需要对象表达式。

See here for more examples. 有关更多示例,请参见此处

I think the closest you're going to get is a delegate (comments are taken from the description of Object Expression that you gave): 我认为你最接近的是委托(评论来自你给出的对象表达的描述):

public delegate int MyDelegate(int input);
// ...is based on an existing base type, interface, or set of interfaces

Then somewhere 然后某个地方

MyDelegate myDelegate = (i) => i * 2;
// An object expression is an expression that creates a new instance of a dynamically created, anonymous object type

myDelegate(1); // returns 2

But this only really deals with signature. 但这只涉及签名。 Methods, properties - I can't think of anything that does those at the moment. 方法,属性 - 我想不出任何可以做到这一点的东西。


Although... 虽然...

One important thing to remember is that you can have F# and C# in the same solution, so you can always write the bulk of your application in C# and do the fiddly bits which can't be done in C# (like this) with F#. 需要记住的一件重要事情是,你可以在同一个解决方案中使用F#和C#,这样你就可以随时用C#编写大部分应用程序,并使用F#执行无法在C#(如此)中完成的繁琐位。

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

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