1

For example I have the following class:

public class PublishFacebookOpenGraphActionRequest
{
    public FacebookOpenGraphActions Action { get; private set; }
    public FacebookOpenGraphObjects ObjectType { get; private set; }
    public Uri Url { get; private set; }
    public Uri Image { get; private set; }
}

And I would like to be able to generate something like this from the current set of properties:

public class PublishFacebookOpenGraphAction
{
    public FacebookOpenGraphActions Action { get; private set; }
    public FacebookOpenGraphObjects ObjectType { get; private set; }
    public Uri Url { get; private set; }
    public Uri Image { get; private set; }

    public PublishFacebookOpenGraphAction WithAction(FacebookOpenGraphActions action)
    {
        this.Action = action;
        return this;
    }

    public PublishFacebookOpenGraphAction WithObjectType(FacebookOpenGraphObjects objectType)
    {
        this.ObjectType = objectType;
        return this;
    }

    public PublishFacebookOpenGraphAction WithUrl(Uri url)
    {
        this.Url = url;
        return this;
    }

    public PublishFacebookOpenGraphAction WithImage(Uri image)
    {
        this.Image = image;
        return this;
    }
}
4

2 回答 2

3

我没有将 PublishFacebookOpenGraphActionRequest 的所有成员转换为流畅的界面。
但也许半自动生成流畅的属性-WithMember-pair 也是向前迈出的一小步。这可以通过ReSharper Live Template实现。

我有一个例子:

在此处输入图像描述

您可以使用该Live Template创建以下代码:

public FacebookOpenGraphActions Action { get; private set; }

public PublishFacebookOpenGraphAction WithAction(FacebookOpenGraphActions action)
{
    this.Action = action;
    return this;
}

只需键入fluentProp+ ENTER并输入属性名称及其类型。

于 2012-05-29T16:37:59.043 回答
2

另一种方法是通过使用正则表达式来使用Visual Studio快速替换( Ctrl+H ) 。 看截图:

在此处输入图像描述

查找什么:设置为::b*public:b+{:i}:b+{:i}:b+\{:b*get;:b*private:b*set;:b*\}:b*$
替换为:设置为:public \1 \2 { get; private set; }\n public _class_ With\2(\1 p\2)\n\{\nthis\.\2 = p\2;\nreturn this;\n\}\n

这会为当前文档中的所有属性添加一个 *With** 成员。不幸的是,我找不到设置 *With** 成员的返回类型的方法。所以我只是将其设置为_class_. 所以你需要_class_用类名替换它(手动或另一个快速替换运行)。

于 2012-05-30T09:02:59.857 回答