代码之家  ›  专栏  ›  技术社区  ›  Wagner DosAnjos

从XML实例化规则后,元素<method>在GetRuleXml()中的方法名称不正确

  •  0
  • Wagner DosAnjos  · 技术社区  · 5 年前

    我动态地操纵规则XML。不幸的是,在我从XML创建规则之后( RuleModel.Create )生成的XML( RuleModel.GetRuleXml() )丢失操作方法名称。我可以通过将规则重新绑定到类型来解决这个问题,但这似乎是多余的,因为该类型已经是Create方法的参数。

    以下是几个XUnit测试来说明这个问题:(CodeEffects 5.0.10.2版)

    using System;
    using CodeEffects.Rule.Attributes;
    using CodeEffects.Rule.Models;
    using Xunit;
    
    namespace ReproSteps
    {
        public class ReproTests
        {
            [Fact]
            public void Should_succeed_but_fails()
            {
                // Arrange
                var rule = RuleModel.Create(ExecutionRuleXml, typeof(ReproType));
    
                rule.IsValid();
    
                // Act
                var ruleXml = rule.GetRuleXml();
    
                // Assert
                // Expected XML should contain: <method name="MyAction">
                // Actual XML contains: <method name="326589FDEC894FF5693EC37EB78FC387">
                Assert.Contains("MyAction", ruleXml); 
            }
    
            [Fact]
            public void Should_succeed_with_rebind()
            {
                // Arrange
                var rule = RuleModel.Create(ExecutionRuleXml, typeof(ReproType));
    
                rule.BindSource(typeof(ReproType)); // this forces the correct method name in the XML
    
                rule.IsValid();
    
                // Act
                var ruleXml = rule.GetRuleXml();
    
                // Assert
                Assert.Contains("MyAction", ruleXml);
            }
    
            private const string ExecutionRuleXml = @"<?xml version=""1.0"" encoding=""utf-8""?><codeeffects xmlns=""http://codeeffects.com/schemas/rule/41"" xmlns:ui=""http://codeeffects.com/schemas/ui/4""><rule id=""__ruleid__"" webrule=""5.0.10.2"" utc=""2020-04-24T20:09:50.5568"" type=""ReproSteps.ReproType, ReproSteps.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"" eval=""false""><name>__rule_name__</name><definition><if><clause><condition type=""equal""><property name=""MyProperty"" /><value type=""string"">A</value></condition></clause><then><method name=""MyAction""><value type=""System.Int32"">1</value></method></then></if></definition><format><lines/></format></rule></codeeffects>";
        }
    
        public class ReproType
        {
            public string MyProperty { get; set; }
    
            [Action]
            public void MyAction(int p)
            {
                Console.WriteLine(p);
            }
        }
    }
    
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   Alex    5 年前

    此问题已在最新的次要版本中修复。更新您的NuGet引用。

    推荐文章