2

我编写了一段简单的代码来参与 Afterthought,但它不起作用,我不知道为什么。其中很大一部分来自其他 SO 问题:How to implement simple Property Amendment with Afterthought

using System;
using System.Collections.Generic;
using Afterthought;
namespace SecondAmendmentTest
{
    class Program
    {
        public class TestUser
        {
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public bool HasChanged { get; set; }
            public void method()
            {
                Console.WriteLine("method");
            }
        }

        public class TestUserAmmendment<T> : Amendment<T, T> where T : TestUser
        {
            public TestUserAmmendment()
            {
                Properties
                    .AfterSet((instance, x, y, z, a) => instance.HasChanged = true);
                Methods.After(ExceptionMethod);
            }

            private object ExceptionMethod(T instance, string method, object[] parameters, object result)
            {
                throw new NotImplementedException();
            }
        }

        static void Main(string[] args)
        {
            TestUser tu = new TestUser();
            Console.WriteLine("Has changed: " + tu.HasChanged.ToString());
            Console.WriteLine("Performing changes...");
            tu.Id = 5;
            tu.FirstName = "name";
            tu.LastName = "lastname";
            Console.WriteLine("Has changed: " + tu.HasChanged.ToString());
            tu.method();
            Console.ReadKey();
        }
    }
}

它可以编译,但不会对输出的 exe 文件进行任何更改。我已经配置了构建后事件。构建输出:

1>------ Rebuild All started: Project: SecondAmendmentTest, Configuration: Debug Any CPU ------
1>  SecondAmendmentTest -> C:\Users\Lukasz\Documents\Visual Studio 11\Projects\SecondAmendmentTest\SecondAmendmentTest\bin\Debug\SecondAmendmentTest.exe
1>  Amending SecondAmendmentTest.exe (5,559 seconds)
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

最后在运行后从应用程序输出:

C:\Users\Lukasz\Documents\Visual Studio 11\Projects\SecondAmendmentTest\SecondAmendmentTest\bin\Debug>SecondAmendmentTest.exe
Has changed: False
Performing changes...
Has changed: False
method

既未修改 HasChanged 属性,也未引发 NotImplementedException。我正在使用 Git 的最后一个资源。你有什么想法为什么它不起作用?

编辑:这是整个解决方案:SecondAmendmentTest.zip

4

2 回答 2

1

我知道这是一年后,但是:

为了以 Willem van Ketwich 的回答为基础,Afterthought.NET 中实际上存在一个错误(疏忽?),如果不修改其父类型,则不允许修改嵌套类型。

我已经提交了修复错误的拉取请求。

除了这个修复,你还需要[Amendment(typeof(TestUserAmmendment<>))]在你的嵌套类上触发修正。

于 2013-08-10T00:34:31.837 回答
0

我通过做两件事来做到这一点:

  • 将 TestUser 和 TestUserAmmendment 类移出 Program 类,这样它们就不会嵌套。

  • 将属性添加[Amendment(typeof(TestUserAmmendment<>))]到 TestUser 类。

工作解决方案可以在这里找到。

于 2012-11-17T04:32:43.073 回答