Mixing types in a Dictionary

I came over to C#.net from coldfusion years ago and one thing we can do in cf that is lovely is we can mix types in a dictionary without any unusual syntax. I recently found myself needing this feature in C# so I built the following class which can store the objects of the same family in the same dictionary. All in all it's a pretty good way to mix types however I'm sure someone else out there can do better.

public class AttributeContainer
{
        public AttributeContainer()
        {
            AttributeCollection = new Dictionary<string, Attribute>();
            AttributeTypeSafety = new Dictionary<string, Type>();
        }
        
        private IDictionary<string, Attribute> AttributeCollection { get; set; }
        private IDictionary<string, Type> AttributeTypeSafety { get; set; }

        public T GetAttribute<t>(string AttributeName) where T : Attribute, new()
        {
            T aType = new T();
            if (!typeof(T).IsAssignableFrom(AttributeTypeSafety[AttributeName]))
            {
                throw new InvalidCastException(string.Format("Attribute:'{0}' can not be cast to type:'{1}'", AttributeName, aType.GetType().ToString()));
            }
            return (T)AttributeCollection[AttributeName];
        }

        public void AddAttribute<t>(T attribute) where T : Attribute, new()
        {
            AttributeCollection.Add(attribute.AttributeName, attribute);
            AttributeTypeSafety.Add(attribute.AttributeName, attribute.GetType());
        }
}

A Dictionary that can store mixed types

I came over to C#.net from coldfusion years ago and one thing we can do in cf that is lovely is we can mix types in a dictionary without any unusual syntax. I recently found myself needing this feature in C# so I built the following class which can store the objects of the same family in the same dictionary. All in all it's a pretty good way to mix types however I'm sure someone else out there can do better.

public class AttributeContainer
{
        public AttributeContainer()
        {
            AttributeCollection = new Dictionary();
            AttributeTypeSafety = new Dictionary();
        }
        
        private IDictionary AttributeCollection { get; set; }
        private IDictionary AttributeTypeSafety { get; set; }

        public T GetAttribute(string AttributeName) where T : Attribute, new()
        {
            T aType = new T();
            if (!typeof(T).IsAssignableFrom(AttributeTypeSafety[AttributeName]))
            {
                throw new InvalidCastException(string.Format("Attribute:'{0}' can not be cast to type:'{1}'", AttributeName, aType.GetType().ToString()));
            }
            return (T)AttributeCollection[AttributeName];
        }

        public void AddAttribute(T attribute) where T : Attribute, new()
        {
            AttributeCollection.Add(attribute.AttributeName, attribute);
            AttributeTypeSafety.Add(attribute.AttributeName, attribute.GetType());
        }
}

USB 2.0 to SATA/IDE Cable Adapter

I have so many IDE hard drives sitting around and I really need to do a backup. Thankfully this quick amazon order should sort me out.  I'll post a review after I get the item.

(Amazon Link)

Using Unity to Implement a Factory Pattern

I could not find any examples of how to use unity to configure a simple factory and return a poco so after many attempts I got this working and I thought I'd share.

First I have generic Factory:

public class ServiceCommandFactory : IServiceCommandFactory
{
 public IServiceCommand CreateInstance() where T :  IServiceCommand, new()
 {
     return new T();
 }
}

Syntax Highlighting

Thanks to Franklin Manuel over at bloggermind.com for posting that article on how to get syntax highlighting enabled.

I currently have brushes for sql, cf, c#, javascript installed and here's the syntax:

<pre class="brush: cf">
<cfset RightNow = Now()>
<cfoutput>
 #RightNow#<br />
 #DateFormat(RightNow)#<br />
 #DateFormat(RightNow,"mm/dd/yy")#<br />
 #TimeFormat(RightNow)#<br />
 #TimeFormat(RightNow,"hh:mm tt")#<br />
 #IsDate(RightNow)#<br />
 #IsDate("January 31, 2007")#<br />
 #IsDate("foo")#<br />
 #DaysInMonth(RightNow)#
</cfoutput>
</pre>

You can get more brushes here:
http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/coldfusion.html

I also use This Tool to convert less than and greater than symbols to html.