Пример #1
0
        /// <summary>
        /// 对 object1 和 object2 进行属性值比较。
        /// </summary>
        /// <param name="objectType"></param>
        /// <param name="object1">要比较的第一个对象。</param>
        /// <param name="object2">要比较的第二个对象。</param>
        /// <param name="ingoreProperties">忽略比较的属性设置。</param>
        /// <returns></returns>
        public bool PropertyEquals <T>(Type objectType, T object1, T object2, Action <ExcludeProperties <T> > ingoreProperties = null)
            where T : class
        {
            var properties = new ExcludeProperties <T>();

            ingoreProperties?.Invoke(properties);
            return(PropertyEquals(typeof(T), object1, object2, properties.PropertyNames.ToArray()));
        }
Пример #2
0
        public bool ShouldCopy(CVariable item)
        {
            if (ExcludeProperties != null &&
                ExcludeProperties.Contains(item.Name))
            {
                return(false);
            }

            return(true);
        }
Пример #3
0
        protected override void OnParametersSet()
        {
            base.OnParametersSet();

            _observeProperties = new List <string>(
                ObserveProperties?.Split(new[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries)
                .WhereNotNull() ?? Array.Empty <string>());

            _excludeProperties = new List <string>(
                ExcludeProperties?.Split(new[] { ' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries)
                .WhereNotNull() ?? Array.Empty <string>());
        }
Пример #4
0
            private void GenerateMethods()
            {
                var bindings = BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance;

                if (!IncludeParentProps)
                {
                    bindings = bindings | BindingFlags.DeclaredOnly;
                }
                var props    = ObjectType.GetProperties(bindings).OrderBy(p => p.Name);
                var excludes = ExcludeProperties == null?null:ExcludeProperties
                               .Where(kv => kv.Key.IsSuperclassOrInterfaceOf(ObjectType))
                               .ToList();

                foreach (var p in props)
                {
                    var pType      = p.PropertyType;
                    var pNameLower = p.Name.ToLowerInvariant();
                    var pIsIndexed = p.GetIndexParameters().Length > 0;
                    var skip       = pIsIndexed || (excludes != null && excludes.Any(typeToNamesPair => typeToNamesPair.Value.Contains(pNameLower)));
                    if (!skip)
                    {
                        var pCleanTypeName = p.PropertyType.ToPrettyTypeName();
                        //custom user converters. E.g String=>Foo, or Foo=>FooMatcher
                        if (AdditionalPropertyMatchers != null)
                        {
                            foreach (var template in AdditionalPropertyMatchers)
                            {
                                if (template.Match(p))
                                {
                                    template.Generate(this, p, MatcherName);
                                }
                            }
                        }
                        var createEquals = EqualMatcherSnippetsByTypeName.ContainsKey(p.PropertyType.FullName);
                        if (createEquals)
                        {
                            var equalMatcherSnippet = EqualMatcherSnippetsByTypeName[p.PropertyType.FullName];
                            var code = equalMatcherSnippet.Replace("$argType", pCleanTypeName).Replace("$argName", "expect").Replace("$propertyName", p.Name);

                            WriteLine();
                            WriteLine("public " + MatcherName + " " + p.Name + "(" + pCleanTypeName + " expect) {");
                            IncrementIndent();
                            if (code.EndsWith(";"))
                            {
                                WriteLine(code);
                            }
                            else
                            {
                                WriteLine(p.Name + "(" + code + ");");
                            }
                            WriteLine("return this;");
                            DecrementIndent();
                            WriteLine("}");
                        }
                        else if (pType.IsEnum)
                        {
                            WriteLine();
                            WriteLine("public " + MatcherName + " " + p.Name + "(" + pCleanTypeName + "? expect) {");
                            IncrementIndent();
                            WriteLine(p.Name + "(AnInstance.EqualTo(expect));");
                            WriteLine("return this;");
                            DecrementIndent();
                            WriteLine("}");
                        }
                        var isNullable      = !pType.IsValueType || Nullable.GetUnderlyingType(pType) != null;
                        var addNullableMark = pType.IsValueType && Nullable.GetUnderlyingType(pType) == null;

                        if (isNullable)
                        {
                            WriteLine();
                            WriteLine("public " + MatcherName + " " + p.Name + "Null() {");
                            IncrementIndent();
                            switch (pCleanTypeName)
                            {
                            case "bool?":
                                WriteLine(p.Name + "(ABool.Null());");
                                break;

                            //case "byte?":
                            //    WriteLine(p.Name + "(AByte.Null());");
                            //    break;
                            case "decimal?":
                                WriteLine(p.Name + "(ADecimal.Null());");
                                break;

                            case "double?":
                                WriteLine(p.Name + "(ADouble.Null());");
                                break;

                            case "float?":
                                WriteLine(p.Name + "(AFloat.Null());");
                                break;

                            case "System.Guid?":
                                WriteLine(p.Name + "(AGuid.Null());");
                                break;

                            case "int?":
                                WriteLine(p.Name + "(AnInt.Null());");
                                break;

                            case "long?":
                                WriteLine(p.Name + "(ALong.Null());");
                                break;

                            case "short?":
                                WriteLine(p.Name + "(AShort.Null());");
                                break;

                            case "string":
                                WriteLine(p.Name + "(AString.Null());");
                                break;

                            default:
                                WriteLine(p.Name + "(AnInstance.EqualTo<" + pCleanTypeName + (addNullableMark ? "?" : "") + ">(null));");
                                break;
                            }
                            WriteLine("return this;");
                            DecrementIndent();
                            WriteLine("}");
                        }

                        WriteLine();
                        WriteLine("public " + MatcherName + " " + p.Name + "(IMatcher<" + pCleanTypeName + (addNullableMark ? "?" : "") + "> matcher) {");
                        IncrementIndent();
                        WriteLine("WithProperty(()=>PropertyNames." + p.Name + ",matcher);");
                        WriteLine("return this;");
                        DecrementIndent();
                        WriteLine("}");
                    }
                }
            }