03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

726 ❘ APPENDIX A Solutions to Exercises<br />

imagePictureBox.ClientSize.Width,<br />

imagePictureBox.ClientSize.Height);<br />

imagePictureBox.Image = Picture;<br />

// Look for assemblies.<br />

foreach (string filename in<br />

Directory.GetFiles(Application.StartupPath, "*.dll"))<br />

{<br />

// Load this assembly.<br />

Assembly dll = Assembly.LoadFile(filename);<br />

// Look for classes that have Draw methods.<br />

foreach (Type type in dll.GetTypes())<br />

{<br />

// Make sure this is a class.<br />

if (!type.IsClass) continue;<br />

// Find the Draw method.<br />

MethodInfo methodInfo = type.GetMethod("Draw");<br />

if (methodInfo == null) continue;<br />

// Make sure it's a static method.<br />

if (!methodInfo.IsStatic) continue;<br />

// Make sure it takes a Graphics object as a parameter.<br />

ParameterInfo[] parameters = methodInfo.GetParameters();<br />

if (parameters.Length != 1) continue;<br />

if (parameters[0].ParameterType != typeof(Graphics)) continue;<br />

// We can use this method!<br />

// Get the class's DisplayName atttribute.<br />

Attribute attribute =<br />

type.GetCustomAttribute(typeof(DisplayNameAttribute));<br />

string toolName;<br />

if (attribute == null) toolName = type.Name;<br />

else<br />

{<br />

DisplayNameAttribute attr = attribute as DisplayNameAttribute;<br />

toolName = attr.DisplayName;<br />

}<br />

// Create a menu item for this method.<br />

ToolStripItem item = toolsMenu.DropDownItems.Add(toolName);<br />

item.Tag = methodInfo;<br />

}<br />

}<br />

}<br />

// Set a Click event handler for the menu item.<br />

item.Click += item_Click;<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!