Write a small application in C#, to output the class names in an assembly.
Tips:
1 – Load the assembly using reflection – Assembly.LoadFrom or something
2 – Get the types in the assembly once loaded
3 – If the type is a class (IsClass or something is there), output it.
Monday, January 14, 2008
Friday, January 11, 2008
Brain Twister for the day
Examine this Clock class we wrote for a game we developed.
public class Clock
{
public delegate void TickDelegate();
private int _milliSeconds = 100;
private bool _stop = false;
private Thread _internalThread = null;
private event TickDelegate Ticks;
///
/// Game clock constructor
///
///
private Clock(TickDelegate t)
{
Ticks += t;
}
///
/// The interval
///
private int Interval
{
get
{
return _milliSeconds;
}
set
{
_milliSeconds = value;
}
}
///
/// StartLoop the clock
///
private void StartLoop()
{
_stop = false;
System.Diagnostics.Debug.WriteLine("Worker Thread Id - " + Thread.CurrentThread.ManagedThreadId);
while (!_stop)
{
Thread.Sleep(_milliSeconds);
if (Ticks != null)
Ticks.Invoke();
}
}
///
/// Start the clock
///
public void Start()
{
_internalThread.Start();
}
///
/// Stop the clock
///
public void Stop()
{
_stop = true;
_internalThread.Join();
}
///
/// Inititialize the game clock
///
///
public static Clock CreateClock(TickDelegate t, int interval)
{
Clock g=new Clock(t);
g.Interval = interval;
g._internalThread = new Thread(new ThreadStart(g.StartLoop));
return g;
}
}
Questions:
1 – How a client instantiate this clock, and obtain the clock ticks by registering an event handler?
2 – Why we need to do _internalThread.Join() when we stop the clock?
public class Clock
{
public delegate void TickDelegate();
private int _milliSeconds = 100;
private bool _stop = false;
private Thread _internalThread = null;
private event TickDelegate Ticks;
///
/// Game clock constructor
///
///
private Clock(TickDelegate t)
{
Ticks += t;
}
///
/// The interval
///
private int Interval
{
get
{
return _milliSeconds;
}
set
{
_milliSeconds = value;
}
}
///
/// StartLoop the clock
///
private void StartLoop()
{
_stop = false;
System.Diagnostics.Debug.WriteLine("Worker Thread Id - " + Thread.CurrentThread.ManagedThreadId);
while (!_stop)
{
Thread.Sleep(_milliSeconds);
if (Ticks != null)
Ticks.Invoke();
}
}
///
/// Start the clock
///
public void Start()
{
_internalThread.Start();
}
///
/// Stop the clock
///
public void Stop()
{
_stop = true;
_internalThread.Join();
}
///
/// Inititialize the game clock
///
///
public static Clock CreateClock(TickDelegate t, int interval)
{
Clock g=new Clock(t);
g.Interval = interval;
g._internalThread = new Thread(new ThreadStart(g.StartLoop));
return g;
}
}
Questions:
1 – How a client instantiate this clock, and obtain the clock ticks by registering an event handler?
2 – Why we need to do _internalThread.Join() when we stop the clock?
Quiz of the day
Identify the purpose of this method.
internal static object ConvertFrom(PropertyInfo pi, string value)
{
TypeConverter tc = TypeDescriptor.GetConverter(pi.PropertyType);
if (tc.CanConvertFrom(typeof(string)))
return tc.ConvertFrom(value);
else
return null; }
internal static object ConvertFrom(PropertyInfo pi, string value)
{
TypeConverter tc = TypeDescriptor.GetConverter(pi.PropertyType);
if (tc.CanConvertFrom(typeof(string)))
return tc.ConvertFrom(value);
else
return null; }
Quiz of the day
What is the significance of this keyword for the string s parameter? What kind of method is IsValidEmailAddress?
public static class AppExtensions{ public static bool IsValidEmailAddress(this string s) { Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"); return regex.IsMatch(s); }
}
public static class AppExtensions{ public static bool IsValidEmailAddress(this string s) { Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"); return regex.IsMatch(s); }
}
Quiz of the day
What is the output of
static void Main(string[] args)
{
int? x = 20;
int? y = null;
int? z = x ?? y;
Console.WriteLine(z);
Console.ReadLine();
}
And why. What is the difference between
int x and int? x
Now, if we can say
int? element2;
//some code
If (element2.HasValue)
//Do something
What is the use of HasValue property?
static void Main(string[] args)
{
int? x = 20;
int? y = null;
int? z = x ?? y;
Console.WriteLine(z);
Console.ReadLine();
}
And why. What is the difference between
int x and int? x
Now, if we can say
int? element2;
//some code
If (element2.HasValue)
//Do something
What is the use of HasValue property?
Quiz of the day
Identify the C# 2.0 features used in the following code.
class Program
{
static void Main(string[] args)
{
List ls=new List(new string[]{"anoop","suresh","rajesh","sachin" });
ls.ForEach(delegate(string s)
{
Console.WriteLine(s);
}
);
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
List
ls.ForEach(delegate(string s)
{
Console.WriteLine(s);
}
);
Console.ReadLine();
}
}
Subscribe to:
Posts (Atom)
