70-511 Get!

Even if I knew most of the stuff already, some materials for study were: the awful MCTS Self-Paced Training Kit for 70-511 and a post from James J. Foster's blog.
switch(obj.GetType())Alas, this is impossible in C# due to the fact that typeof(class) is not considered a constant, but a method call.
{
case typeof(MyClass1):
// do something with obj
break;
case typeof(MyClass2):
// do something else with obj
break;
default:
throw new NotSupportedException();
}
class Program
{
public class BaseClass
{
}
public class InheritedClass1 : BaseClass
{
}
public class InheritedClass2 : BaseClass
{
}
public class TypeCatchBlockException : Exception
{
public static void Throw(object obj)
{
doThrow((dynamic)obj);
}
private static void doThrow<T>(T obj)
{
throw new TypeCatchBlockException<T>();
}
/*public static void Throw(object obj)
{
var type=obj==null?null:obj.GetType();
var exceptionTypeName=typeof(TypeCatchBlockException)+"`1";
Type generic=Type.GetType(exceptionTypeName).MakeGenericType(new[]{type});
throw (Exception)Activator.CreateInstance(generic);
}*/
}
public class TypeCatchBlockException<T> : TypeCatchBlockException
{
}
static void Main(string[] args)
{
showObjectType(new InheritedClass2());
showObjectType(new BaseClass());
showObjectType(new InheritedClass1());
showObjectType(new object());
Console.ReadLine();
}
private static void showObjectType(object obj)
{
try
{
TypeCatchBlockException.Throw(obj);
}
catch (TypeCatchBlockException<BaseClass>)
{
Console.WriteLine("BaseClass object");
}
catch (TypeCatchBlockException<InheritedClass1>)
{
Console.WriteLine("InheritedClass1 object");
}
catch (TypeCatchBlockException<InheritedClass2>)
{
Console.WriteLine("InheritedClass2 object");
}
catch (TypeCatchBlockException)
{
Console.WriteLine("Unsupported object");
}
}
}
InheritedClass2 object
BaseClass object
InheritedClass1 object
Unsupported object
/// <summary>
/// Helper for Image objects
/// </summary>
public static class ImageHelper
{
/// <summary>
/// Get an image from a byte array
/// </summary>
/// <param name="iconBytes"></param>
/// <returns></returns>
public static Image GetImageFromBytes(byte[] iconBytes)
{
if (iconBytes == null || iconBytes.Length == 0)
return null;
using (MemoryStream ms = new MemoryStream(iconBytes))
{
try
{
return GetImageFromStream(ms);
}
catch
{
return null;
}
}
}
/// <summary>
/// Get an image from a file without locking the file
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static Image GetImageFromFile(string fileName)
{
try
{
using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
return GetImageFromStream(stream);
}
}
catch
{
return null;
}
}
/// <summary>
/// Determines if an image is indexed (with a color palette)
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static bool IsImageIndexed(Image image)
{
switch (image.PixelFormat)
{
case PixelFormat.Format1bppIndexed:
case PixelFormat.Format4bppIndexed:
case PixelFormat.Format8bppIndexed:
case PixelFormat.Indexed:
return true;
}
return false;
}
/// <summary>
/// Get an image from stream without locking the stream
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static Image GetImageFromStream(Stream stream)
{
try
{
using (var image = Image.FromStream(stream))
{
var srcBitmap = image as Bitmap;
var destBitmap = new Bitmap(image.Width, image.Height,image.PixelFormat);
if (IsImageIndexed(image)&&srcBitmap!=null)
{
Rectangle srcArea = new Rectangle(0, 0, image.Width, image.Height);
BitmapData srcData = srcBitmap.LockBits(srcArea, ImageLockMode.ReadOnly, image.PixelFormat);
Rectangle destArea = new Rectangle(0, 0, image.Width, image.Height);
BitmapData destData = destBitmap.LockBits(destArea, ImageLockMode.WriteOnly, image.PixelFormat);
IntPtr srcPtr = srcData.Scan0;
IntPtr destPtr = destData.Scan0;
byte[] buffer = new byte[srcData.Stride*image.Height];
Marshal.Copy(srcPtr, buffer, 0, buffer.Length);
Marshal.Copy(buffer, 0, destPtr, buffer.Length);
srcBitmap.UnlockBits(srcData);
destBitmap.UnlockBits(destData);
destBitmap.Palette = srcBitmap.Palette;
} else
{
using (var graphics = Graphics.FromImage(destBitmap))
{
graphics.DrawImage(image, 0, 0);
}
}
return destBitmap;
}
}
catch
{
return null;
}
}
}
From early ages we learn to listen to the people around us, but only at a later time we get to understand that what those people are saying may not be true or in our advantage. I believe this sets us up for the rest of life to partially believe even the most ridiculous claims for the sole reason that someone has uttered them. And who better to take advantage of this than politicians and lawyers.
Check out these stupid ideas:
The list could go on and on and on, but these were both startlingly ridiculous and pretty recent. Apparently, the DRM bug, the one that made people sell you things and then consider it's still their property, is still alive and thriving. Most such beliefs come from the power we give other by not reacting. It is easier for us to do nothing than do something, so it is easy to take advantage by positioning the average human so that he has to do something to thwart your plans.