Click here to Skip to main content
15,891,529 members
Articles / General Programming / Algorithms

Jazz Up Your C# Code

Rate me:
Please Sign up or sign in to vote.
4.95/5 (11 votes)
3 Jul 2012CPOL28 min read 51.6K   369   54  
Maintaining code with complex permissions tends to be difficult, because the code can be distributed across multiple classes. By embedding permissions directly on methods and properties within a class, code is reduced and maintainability is simplified.
/*
 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
*/

using System;

namespace C5
{
    struct RecConst
    {
        public const int HASHFACTOR = 387281;
    }
    /// <summary>
    /// A generic record type with two fields. 
    /// <para>
    /// Equality is defined field by field, using the <code>Equals</code> method 
    /// inherited from <code>System.Object</code> (i.e. using <see cref="T:C5.NaturalEqualityComparer`1"/>).
    /// </para>
    /// <para>
    /// This type is similar to <see cref="T:C5.KeyValuePair`2"/>, but the latter
    /// uses <see cref="P:C5.EqualityComparer`1.Default"/> to define field equality instead of <see cref="T:C5.NaturalEqualityComparer`1"/>.
    /// </para>
    /// </summary>
    /// <typeparam name="T1"></typeparam>
    /// <typeparam name="T2"></typeparam>
    public struct Rec<T1, T2> : IEquatable<Rec<T1, T2>>, IShowable
    {
        /// <summary>
        /// 
        /// </summary>
        public readonly T1 X1;
        /// <summary>
        /// 
        /// </summary>
        public readonly T2 X2;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="x2"></param>
        public Rec(T1 x1, T2 x2)
        {
            this.X1 = x1; this.X2 = x2;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(Rec<T1, T2> other)
        {
            return
              (X1 == null ? other.X1 == null : X1.Equals(other.X1)) &&
              (X2 == null ? other.X2 == null : X2.Equals(other.X2))
              ;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            return obj is Rec<T1, T2> ? Equals((Rec<T1, T2>)obj) : false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="record1"></param>
        /// <param name="record2"></param>
        /// <returns></returns>
        public static bool operator ==(Rec<T1, T2> record1, Rec<T1, T2> record2)
        {
            return record1.Equals(record2);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="record1"></param>
        /// <param name="record2"></param>
        /// <returns></returns>
        public static bool operator !=(Rec<T1, T2> record1, Rec<T1, T2> record2)
        {
            return !record1.Equals(record2);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            //TODO: don't use 0 as hashcode for null, but something else!
            int hashcode = X1 == null ? 0 : X1.GetHashCode();
            hashcode = hashcode * RecConst.HASHFACTOR + (X2 == null ? 0 : X2.GetHashCode());
            return hashcode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("({0}, {1})", X1, X2);
        }

        #region IShowable Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stringbuilder"></param>
        /// <param name="rest"></param>
        /// <param name="formatProvider"></param>
        /// <returns></returns>
        public bool Show(System.Text.StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
        {
            bool incomplete = true;
            stringbuilder.Append("(");
            rest -= 2;
            try
            {
                if (incomplete = !Showing.Show(X1, stringbuilder, ref rest, formatProvider))
                    return false;
                stringbuilder.Append(", ");
                rest -= 2;
                if (incomplete = !Showing.Show(X2, stringbuilder, ref rest, formatProvider))
                    return false;
            }
            finally
            {
                if (incomplete)
                {
                    stringbuilder.Append("...");
                    rest -= 3;
                }
                stringbuilder.Append(")");
            }
            return true;
        }
        #endregion

        #region IFormattable Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="format"></param>
        /// <param name="formatProvider"></param>
        /// <returns></returns>
        public string ToString(string format, IFormatProvider formatProvider)
        {
            return Showing.ShowString(this, format, formatProvider);
        }

        #endregion
    }
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T1"></typeparam>
    /// <typeparam name="T2"></typeparam>
    /// <typeparam name="T3"></typeparam>
    public struct Rec<T1, T2, T3> : IEquatable<Rec<T1, T2, T3>>, IShowable
    {
        /// <summary>
        /// 
        /// </summary>
        public readonly T1 X1;
        /// <summary>
        /// 
        /// </summary>
        public readonly T2 X2;
        /// <summary>
        /// 
        /// </summary>
        public readonly T3 X3;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="x2"></param>
        /// <param name="x3"></param>
        public Rec(T1 x1, T2 x2, T3 x3)
        {
            this.X1 = x1; this.X2 = x2; this.X3 = x3;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(Rec<T1, T2, T3> other)
        {
            return
              (X1 == null ? other.X1 == null : X1.Equals(other.X1)) &&
              (X2 == null ? other.X2 == null : X2.Equals(other.X2)) &&
              (X3 == null ? other.X3 == null : X3.Equals(other.X3))
              ;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            return obj is Rec<T1, T2, T3> ? Equals((Rec<T1, T2, T3>)obj) : false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="record1"></param>
        /// <param name="record2"></param>
        /// <returns></returns>
        public static bool operator ==(Rec<T1, T2, T3> record1, Rec<T1, T2, T3> record2)
        {
            return record1.Equals(record2);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="record1"></param>
        /// <param name="record2"></param>
        /// <returns></returns>
        public static bool operator !=(Rec<T1, T2, T3> record1, Rec<T1, T2, T3> record2)
        {
            return !record1.Equals(record2);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            //TODO: don't use 0 as hashcode for null, but something else!
            int hashcode = X1 == null ? 0 : X1.GetHashCode();
            hashcode = hashcode * RecConst.HASHFACTOR + (X2 == null ? 0 : X2.GetHashCode());
            hashcode = hashcode * RecConst.HASHFACTOR + (X3 == null ? 0 : X3.GetHashCode());
            return hashcode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("({0}, {1}, {2})", X1, X2, X3);
        }
        #region IShowable Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stringbuilder"></param>
        /// <param name="rest"></param>
        /// <param name="formatProvider"></param>
        /// <returns></returns>
        public bool Show(System.Text.StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
        {
            bool incomplete = true;
            stringbuilder.Append("(");
            rest -= 2;
            try
            {
                if (incomplete = !Showing.Show(X1, stringbuilder, ref rest, formatProvider))
                    return false;
                stringbuilder.Append(", ");
                rest -= 2;
                if (incomplete = !Showing.Show(X2, stringbuilder, ref rest, formatProvider))
                    return false;
                stringbuilder.Append(", ");
                rest -= 2;
                if (incomplete = !Showing.Show(X3, stringbuilder, ref rest, formatProvider))
                    return false;
            }
            finally
            {
                if (incomplete)
                {
                    stringbuilder.Append("...");
                    rest -= 3;
                }
                stringbuilder.Append(")");
            }
            return true;
        }
        #endregion

        #region IFormattable Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="format"></param>
        /// <param name="formatProvider"></param>
        /// <returns></returns>
        public string ToString(string format, IFormatProvider formatProvider)
        {
            return Showing.ShowString(this, format, formatProvider);
        }

        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T1"></typeparam>
    /// <typeparam name="T2"></typeparam>
    /// <typeparam name="T3"></typeparam>
    /// <typeparam name="T4"></typeparam>
    public struct Rec<T1, T2, T3, T4> : IEquatable<Rec<T1, T2, T3, T4>>, IShowable
    {
        /// <summary>
        /// 
        /// </summary>
        public readonly T1 X1;
        /// <summary>
        /// 
        /// </summary>
        public readonly T2 X2;
        /// <summary>
        /// 
        /// </summary>
        public readonly T3 X3;
        /// <summary>
        /// 
        /// </summary>
        public readonly T4 X4;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="x2"></param>
        /// <param name="x3"></param>
        /// <param name="x4"></param>
        public Rec(T1 x1, T2 x2, T3 x3, T4 x4)
        {
            this.X1 = x1; this.X2 = x2; this.X3 = x3; this.X4 = x4;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(Rec<T1, T2, T3, T4> other)
        {
            return
              (X1 == null ? other.X1 == null : X1.Equals(other.X1)) &&
              (X2 == null ? other.X2 == null : X2.Equals(other.X2)) &&
              (X3 == null ? other.X3 == null : X3.Equals(other.X3)) &&
              (X4 == null ? other.X4 == null : X4.Equals(other.X4))
              ;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            return obj is Rec<T1, T2, T3, T4> ? Equals((Rec<T1, T2, T3, T4>)obj) : false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="record1"></param>
        /// <param name="record2"></param>
        /// <returns></returns>
        public static bool operator ==(Rec<T1, T2, T3, T4> record1, Rec<T1, T2, T3, T4> record2)
        {
            return record1.Equals(record2);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="record1"></param>
        /// <param name="record2"></param>
        /// <returns></returns>
        public static bool operator !=(Rec<T1, T2, T3, T4> record1, Rec<T1, T2, T3, T4> record2)
        {
            return !record1.Equals(record2);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            //TODO: don't use 0 as hashcode for null, but something else!
            int hashcode = X1 == null ? 0 : X1.GetHashCode();
            hashcode = hashcode * RecConst.HASHFACTOR + (X2 == null ? 0 : X2.GetHashCode());
            hashcode = hashcode * RecConst.HASHFACTOR + (X3 == null ? 0 : X3.GetHashCode());
            hashcode = hashcode * RecConst.HASHFACTOR + (X4 == null ? 0 : X4.GetHashCode());
            return hashcode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.Format("({0}, {1}, {2}, {3})", X1, X2, X3, X4);
        }
        #region IShowable Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stringbuilder"></param>
        /// <param name="rest"></param>
        /// <param name="formatProvider"></param>
        /// <returns></returns>
        public bool Show(System.Text.StringBuilder stringbuilder, ref int rest, IFormatProvider formatProvider)
        {
            bool incomplete = true;
            stringbuilder.Append("(");
            rest -= 2;
            try
            {
                if (incomplete = !Showing.Show(X1, stringbuilder, ref rest, formatProvider))
                    return false;
                stringbuilder.Append(", ");
                rest -= 2;
                if (incomplete = !Showing.Show(X2, stringbuilder, ref rest, formatProvider))
                    return false;
                stringbuilder.Append(", ");
                rest -= 2;
                if (incomplete = !Showing.Show(X3, stringbuilder, ref rest, formatProvider))
                    return false;
                stringbuilder.Append(", ");
                rest -= 2;
                if (incomplete = !Showing.Show(X4, stringbuilder, ref rest, formatProvider))
                    return false;
            }
            finally
            {
                if (incomplete)
                {
                    stringbuilder.Append("...");
                    rest -= 3;
                }
                stringbuilder.Append(")");
            }
            return true;
        }
        #endregion

        #region IFormattable Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="format"></param>
        /// <param name="formatProvider"></param>
        /// <returns></returns>
        public string ToString(string format, IFormatProvider formatProvider)
        {
            return Showing.ShowString(this, format, formatProvider);
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer TrackerRealm
Canada Canada
Charles Wiebe and John Hansen are Microsoft .NET software architect/designer, for Windows Forms and ASP.NET solutions.
Charles specializes in the application layer – Web Parts and other ways of building GUI’s. John specializes in high capacity object oriented systems and connectivity.

John and Charles are co-developers of Jetfire – a .net open source, multi-user, application domain specific language (DSL) with syntax heavily based on C# and Java. The goal of Jetfire is to allow power users to quickly and easily develop and deploy applications, in much the same way as Excel allows powers users to quickly develop spread sheets.

Their latest project is Jazz - a compact, modular framework that allows new, or existing, applications to easily employ roles, states, ACLs and void safety. Jazz allows complete workflows to be implemented in only a few hundred lines of C# or VB code.

Comments and Discussions