Tuesday, 27 March 2012

Security Testing

Imagine you have an important website, how could you make sure that it's safe in all aspects of it?!

this Company can help: http://www.portcullis-security.com/ running security tests on your site including SQL Injection, etc.

Monday, 19 March 2012

An Extension Method to Convert a Type to another Type

public static class ObjectExtensions
public static class ObjectExtensions
    {
        public static T To(this object obj)
        {
            if (obj == DBNull.Value || obj == null)
            {
                return default(T);
            }

            Type newType = typeof(T);
            Type underLyingNewType = Nullable.GetUnderlyingType(newType);

            if (underLyingNewType == null)
            {
                // it means it's not a nullable type, so just convert it as normal
                return (T)Convert.ChangeType(obj, newType);
            }

            // it's a nullable type so convert it to the underlying type.
            return (T)Convert.ChangeType(obj, underLyingNewType);
        } 
    }
 
And you can use it simply like:
 
[Test]
        public void TypeConversionTest()
        {
            var dbNullValue = DBNull.Value;
            var stringValue = "12";

            var convertedToDateTime = dbNullValue.To();
            var convertedToNullableDateTime = dbNullValue.To();
            var stringConvertedToNullableInt = stringValue.To();
        }