9/30/2009

Generic Enum "Composed Of" Test

UPDATE: Microsoft will be offering a substitute for this method in .NET Framework 4, so you could say that this post is obsolete.

I was working with Windows ACL permissions and I was attempting to find out whether a user had Read permissions to a file. Permissions are represented in the form of an enum to something of this effect:


To further complicate things, Windows permissions are represented under 2 different enum classes, System.IO.FileAccess and System.Security.AccessControl.FileSystemRights. So, in order to persist these permissions to the database, I chose to also persist the respective enum class in the form of text, and then pull it back out of the database and perform Enum.Parse() on the text.

Whenever a file had Modify permissions, there was no concise way of determining that this also implied the Read permission because I was dealing with the generic Enum class. As you can see, when you attempt bitwise operations on the Enum class, this is what you get:


Error: Operator '&' cannot be applied to operands of type 'System.Enum' and 'System.Enum'

So, in order to get around this, I chose to cast the Enums to a type that I could use bitwise operations on, which lead me to generate this function:

public static bool EnumIsCompositeOf(Enum little, Enum big)
{
    if (!Type.Equals(little.GetType(), big.GetType()))
        throw new ArgumentException("Enums must be of the same type.");

    ulong littleVal;
    ulong bigVal;

    TypeCode typeCode = little.GetTypeCode();
    switch (typeCode)
    {
        case TypeCode.Byte:
        case TypeCode.UInt16:
        case TypeCode.UInt32:
        case TypeCode.UInt64:
            littleVal = ulong.Parse(little.ToString("D"));
            bigVal = ulong.Parse(big.ToString("D"));
            break;
        case TypeCode.SByte:
        case TypeCode.Int16:
        case TypeCode.Int32:
        case TypeCode.Int64:
            littleVal = (ulong)long.Parse(little.ToString("D"));
            bigVal = (ulong)long.Parse(big.ToString("D"));
            break;
        default:
            throw new ArgumentException("Enums are derived from an unknown type.");
    }

    ulong intersection = littleVal & bigVal;
    return intersection == littleVal ? true : false;
}

9/12/2009

I guess blogs don't totally suck


I never thought that I would some day own a blog.  I mean, who gives a crap about what I think?  Hell, I don't even care about what I think.  In my mind, blogs were just a byproduct of babbling buffoons who ran out of humans to tolerated their rambling.  And, well, not much has changed.  But on to my point...

As a software developer for a large company, I shamelessly admit that I "google" answers to problems probably upwards to 50 times per day.  When my employer used a web filter to block access to blogs I became handicapped.  Half of the time I tried to "google it," I was blocked.  Holy shit, my job just got a lot harder.  I suddenly realized that most of the valuable information I found online was actually in a blog.

From that point forward I took on a whole new stance on blogs.  Sure, 90% of blogs are a waste of life, but my life would be a hell of a lot harder without that other 10%.

I realized that I, too, possess valuable information that the rest of the world could use.  We all contain little random turds of useful knowledge that nobody else knows about - it's not necessarily stuff people want to read every day, but by posting it online you expose it to search engines and potentially billions of people.

By creating this blog, I hope that I can give back to the world that has helped me so much over the years.  Here's to a blog that doesn't suck!