CsCheck/Utils.cs
/// <summary>Default model equal implementation. Handles most collections ordered when actual is IList like or unordered when actual is ICollection based.</summary>
Default model equal implementation. Handles most collections ordered when actual is IList like or unordered when actual is ICollection based.
Default model equal implementation . Handles most collections ordered when actual is IList like or unordered when actual is ICollection based .
public static bool ModelEqual<T, M>(T actual, M model)
        {
            if (actual is IList ail && model is IList bil)
            {
                if (ail.Count != bil.Count) return false;
                for (int i = 0; i < ail.Count; i++)
                    if (!ail[i].Equals(bil[i]))
                        return false;
                return true;
            }
            else if (actual.GetType().GetInterface(typeof(IReadOnlyList<>).Name) != null
                  && model.GetType().GetInterface(typeof(IReadOnlyList<>).Name) != null)
            {
                var e1 = ((IEnumerable)actual).GetEnumerator();
                var e2 = ((IEnumerable)model).GetEnumerator();
                while (true)
                {
                    var e1MoveNext = e1.MoveNext();
                    if (e1MoveNext != e2.MoveNext()) return false;
                    if (!e1MoveNext) return true;
                    if (!Equal(e1.Current, e2.Current)) return false;
                }
            }
            else if (actual is ICollection aic && model is ICollection bic)
            {
                return aic.Count == bic.Count && !aic.Cast<object>().Except(bic.Cast<object>()).Any();
            }
            else if (actual is IEnumerable aie && model is IEnumerable bie)
            {
                var aieo = aie.Cast<object>().ToList();
                var bieo = bie.Cast<object>().ToList();
                return aieo.Count == bieo.Count && !aieo.Except(bieo).Any();
            }
            return actual.Equals(model);
        }
public static bool ModelEqual < T , M > ( T actual , M model ) { if ( actual is IList ail && model is IList bil ) { if ( ail . Count != bil . Count ) return false ; for ( int i = 0 ; i < ail . Count ; i ++ ) if ( ! ail [ i ] . Equals ( bil [ i ] ) ) return false ; return true ; } else if ( actual . GetType ( ) . GetInterface ( typeof ( IReadOnlyList < > ) . Name ) != null && model . GetType ( ) . GetInterface ( typeof ( IReadOnlyList < > ) . Name ) != null ) { var e1 = ( ( IEnumerable ) actual ) . GetEnumerator ( ) ; var e2 = ( ( IEnumerable ) model ) . GetEnumerator ( ) ; while ( true ) { var e1MoveNext = e1 . MoveNext ( ) ; if ( e1MoveNext != e2 . MoveNext ( ) ) return false ; if ( ! e1MoveNext ) return true ; if ( ! Equal ( e1 . Current , e2 . Current ) ) return false ; } } else if ( actual is ICollection aic && model is ICollection bic ) { return aic . Count == bic . Count && ! aic . Cast < object > ( ) . Except ( bic . Cast < object > ( ) ) . Any ( ) ; } else if ( actual is IEnumerable aie && model is IEnumerable bie ) { var aieo = aie . Cast < object > ( ) . ToList ( ) ; var bieo = bie . Cast < object > ( ) . ToList ( ) ; return aieo . Count == bieo . Count && ! aieo . Except ( bieo ) . Any ( ) ; } return actual . Equals ( model ) ; }
Default model equal implementation.
Default model equal implementation .
actual
T
model
M