事后思考如何实施简单的财产修正​​案 [英] How to implement simple Property Ammendment with Afterthought

查看:75
本文介绍了事后思考如何实施简单的财产修正​​案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试事后思考.我想在调用任何属性设置器时设置变量HasChanged.我有以下代码

I'm trying to figure out Afterthought. I want to set the variable HasChanged when any property setter is called. I have the following code

public class TestUser
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool HasChanged { get; set; }
}

public class TestUserAmmendment<T> : Amendment<T, T> where T : TestUser
{
    public TestUserAmmendment()
    {
        Properties
            .Where(p => p.PropertyInfo.CanRead
                && p.PropertyInfo.CanWrite
                && p.PropertyInfo.GetSetMethod().IsPublic
                && p.Name != "HasChanged")
            .AfterSet(instance.HasChanged = true);
    }
}

但是我收到一个编译器错误,提示The name 'Properties' does not exist in the current context.我已经从单元测试中复制了代码,可以对其进行编译和运行.我不确定,但是没有人知道我可以使用Afterthought实现这一简单方面吗?

but I am receiving a compiler error saying The name 'Properties' does not exist in the current context. I have copied the code from the unit tests, which I can compile and run. I'm not sure, but does anyone know how I can implement this simple aspect using Afterthought?

推荐答案

您的示例代码将为我编译.

Your sample code compiles for me.

我相信直到最新版本1.0.8才添加流畅的界面.尝试将其更新到最新版本,如果仍然有问题,我会帮您解决.

The fluent interface was not added, I believe, until version 1.0.8, which is the most current version. Try updating to the latest version, and if you are still having problems, I'll see if I can help further.

修改 实际上,我确实必须进行一次更改才能使它在我第一次尝试时就可以编译.我将AfterSet更改为:

Edit I actually did have to make one change to get it to compile the first time I tried. I changed the AfterSet to:

.AfterSet((instance, x, y, z, a) => instance.HasChanged = true);

但是,由于这实际上并不是问题的一部分,因此我立即忘记了它. :)

But since that wasn't really part of the question, I promptly forgot about it. :)

这篇关于事后思考如何实施简单的财产修正​​案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆