David Findley's Blog

My little home in the cloud.

  • Back on track.

    It's been a long summer. My day job has kept me away from blogging.

  • MS is creating a new functional language called F#

    I found this while making my rounds on the web this morning. MS research is creating a new functional language called F# that seems to be based on OCaml and C#. I'm still not sure of the practical uses for most of us but I still cant help but have a morbid fascination with the more academic languages. :) 

  • phUn with x0r

    This is a wacky idea I had recently about a use of the reflexive nature of XOR. For those of you that don't remember XOR has this interesting behavior:

    If A XOR B = C then B XOR C = A and A XOR C = B.

    In otherwords if you have any two values you can get the third by XORing the two that you have.

  • Writing regex that doesn't get you beat up in the parking lot after work.

    I have recently become enlightened in the ways of regex. Regex has really allowed me to do some things that would have been very difficult to do with just string manipulation. But alas, I am gaining a reputation for churning out incomprehensible code because of the regex that is involved. There is a great article over on the Show Us Your Code site that reminded me that sometimes its not cool to "do it in one line of code".

  • Ahh the good ol' days.

    One of the very first computers I actually owned myself was a timex sinclair. I remember the days of writing a hex loader in basic so that I could hand POKE some assembly into the darn thing. Maybe in C# 2.0 they can add a poke keyword! bwahaha. Check out this great zx-spectrum emulator and let the nastalgia flow.

  • Authorization Extender

    [ProvideProperty("Roles", typeof(Control))]
     [DesignerSerializer(typeof(AuthExtenderCodeDomSerializer), typeof(CodeDomSerializer))]
     public class AuthExtender : System.ComponentModel.Component, IExtenderProvider
     {
      #region IExtenderProvider Members

      public bool CanExtend(object extendee)
      {
       if (extendee is AuthExtender || extendee is Page)
        return false;
       return true;
      }

      #endregion

      internal Hashtable _roles = new Hashtable();
      public string GetRoles(Control c)
      {
       return _roles[c] as string;
      }

      public void SetRoles(Control c, string roles)
      {
       _roles[c] = roles;
       c.PreRender += new EventHandler(Control_OnPreRender);
      }

      private void Control_OnPreRender(object sender, EventArgs e)
      {
       Control c = sender as Control;
       if (c.Visible)
       {
        string[] roles = ((string)_roles[c]).Split(',');
        c.Visible = false;
        foreach (string role in roles)
        {
         if (HttpContext.Current.User.IsInRole(role))
         {
          c.Visible = true;
          break;
         }
        }
       }
      }
     }


     internal class AuthExtenderCodeDomSerializer : CodeDomSerializer
     {
      public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
      {
       // This is how we associate the component with the serializer.
       CodeDomSerializer baseClassSerializer =
        (CodeDomSerializer)manager.GetSerializer(
         typeof(AuthExtender).BaseType, typeof(CodeDomSerializer));

       object root = baseClassSerializer.Deserialize(manager, codeObject);

       if (codeObject is CodeStatementCollection)
       {
        CodeStatementCollection statements = (CodeStatementCollection)codeObject;

        foreach (CodeStatement statement in statements)
        {
         // if the statement is an expression it should be
         // the SetRoles method invoke expression
         if (statement is CodeExpressionStatement)
         {
          base.DeserializeStatement(manager, statement);
         }
        }
       }

       return root;
      }
     
      public override object Serialize(IDesignerSerializationManager manager, object value)
      {
       CodeDomSerializer baseClassSerializer =
        (CodeDomSerializer)manager.GetSerializer(
         typeof(AuthExtender).BaseType, typeof(CodeDomSerializer));
     
       object codeObject = baseClassSerializer.Serialize(manager, value);
       if (codeObject is CodeStatementCollection)
       {
        CodeStatementCollection statements = (CodeStatementCollection)codeObject;
     
        string commentText = "Setup the authorization for the controls on the form.";
        CodeCommentStatement comment = new CodeCommentStatement(commentText);
        statements.Add(comment);

        AuthExtender ext = value as AuthExtender;
        foreach (Control c in ext._roles.Keys)
        {
         string roles = ext._roles[c] as string;
         if (roles != null && roles != "")
         {
          CodeMethodInvokeExpression methodInvoke = new CodeMethodInvokeExpression(
           // targetObject that contains the method to invoke.
           new CodePropertyReferenceExpression(
           new CodeThisReferenceBLOCKED EXPRESSION,
           manager.GetName(ext)
           ),
           // methodName indicates the method to invoke.
           "SetRoles",
           // parameters array contains the parameters for the method.
           new CodeExpression[]
           {
            new CodePropertyReferenceExpression(
             new CodeThisReferenceBLOCKED EXPRESSION,
             c.ID
            ),
            new CodePrimitiveExpression(
             ext._roles[c]
            )
           }
          );
          statements.Add(methodInvoke);
         }
        }
       }
       return codeObject;
      }
     }