Using reflection to get information about an assembly

image_pdfimage_print
   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

// Asy.cs -- Demonstrates using reflection to get information
//           about an assembly.
//
//           Compile this program with the following command line:
//               C:>csc Asy.cs
using System;
using System.Reflection;

public class Asy
{
    static public void Main ()
    {
        Assembly asy = null;
        try
        {
            asy = Assembly.Load ("Circle");
        }
        catch (Exception e)
        {
            Console.WriteLine (e.Message);
            return;
        }
        Type [] types = asy.GetTypes();
        foreach (Type t in types)
            ShowTypeInfo (t);
    }
    static void ShowTypeInfo (Type t)
    {
            Console.WriteLine ("{0} is a member of the {1} namespace", t.Name, t.Namespace);
            Console.WriteLine ("
Methods in {0}:", t.Name);
            MethodInfo [] methods = t.GetMethods ();
            foreach (MethodInfo m in methods)
                Console.WriteLine ("	" + m.Name);
            Console.WriteLine ("
Properties in {0}:", t.Name);
            PropertyInfo [] props = t.GetProperties ();
            foreach (PropertyInfo p in props)
                Console.WriteLine ("	" + p.Name);
            Console.WriteLine ("
Fields in {0}:", t.Name);
            FieldInfo [] fields = t.GetFields ();
            foreach (FieldInfo f in fields)
                Console.WriteLine ("	" + f.Name);
    }
}


           
          


Random Color and Rectangle

image_pdfimage_print
   
 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class RandomRectangle: Form
{
     public static void Main()
     {
          Application.Run(new RandomRectangle());
     }
     public RandomRectangle()
     {
          Text = "Random Rectangle";
   
          Timer timer    = new Timer();
          timer.Interval = 1;
          timer.Tick    += new EventHandler(TimerOnTick);
          timer.Start();
     }
     void TimerOnTick(object obj, EventArgs ea)
     {
          Random rand = new Random();
   
          int x1 = rand.Next(ClientSize.Width);
          int x2 = rand.Next(ClientSize.Width);
          int y1 = rand.Next(ClientSize.Height);
          int y2 = rand.Next(ClientSize.Height);
   
          Color color = Color.FromArgb(rand.Next(256), 
                                       rand.Next(256), 
                                       rand.Next(256));
   
          Graphics grfx = CreateGraphics();
          grfx.FillRectangle(new SolidBrush(color), 
                             Math.Min(x1,  x2), Math.Min(y1,  y2),
                             Math.Abs(x2 - x1), Math.Abs(y2 - y1));
          grfx.Dispose();
     }
}

    


Roll a six-sided die 6000 times.

image_pdfimage_print

using System;

public class RollDie
{
public static void Main( string[] args )
{
Random randomNumbers = new Random();

int frequency1 = 0;
int frequency2 = 0;
int frequency3 = 0;
int frequency4 = 0;
int frequency5 = 0;
int frequency6 = 0;

int face;

for ( int roll = 1; roll <= 6000; roll++ ) { face = randomNumbers.Next( 1, 7 ); switch ( face ) { case 1: frequency1++; break; case 2: frequency2++; break; case 3: frequency3++; break; case 4: frequency4++; break; case 5: frequency5++; break; case 6: frequency6++; break; } } Console.WriteLine( "Face Frequency" ); // output headers Console.WriteLine( "1 {0} 2 {1} 3 {2} 4 {3} 5 {4} 6 {5}", frequency1, frequency2, frequency3, frequency4, frequency5, frequency6 ); } } [/csharp]

Get Random number

image_pdfimage_print
   
 
using System;

class CondOpApp {
    [STAThread]
    static void Main(string[] args) {
        Random rand = new Random();
        int a = 0, b = 0;

        for (int i = 0; i < 5; i++) {
            a = rand.Next() % 100;
            b = rand.Next() % 100;

            Console.WriteLine("a={0}, b={1}, so the winner is: {2}", a, b, a > b ? &#039;a&#039; : &#039;b&#039;);
        }
    }
}

    


ProcessStartInfo:FileName,Arguments,WorkingDirectory,WindowStyle,ErrorDialog

image_pdfimage_print
   
 
using System;
using System.Diagnostics;

class MainClass
{
    public static void Main()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "notepad.exe";
        startInfo.Arguments = "file.txt";
        startInfo.WorkingDirectory = @"C:Temp";
        startInfo.WindowStyle = ProcessWindowStyle.Maximized;
        startInfo.ErrorDialog = true;

        Process process;
        try
        {
            process = Process.Start(startInfo);
            Console.WriteLine("Waiting 30 seconds for process to finish.");
            if (process.WaitForExit(30000))
            {
                Console.WriteLine("Process terminated.");
            }
            else
            {
                Console.WriteLine("Timed out waiting for process to end.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Could not start process.");
            Console.WriteLine(ex);
        }
    }
}