Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 5th, 2012  |  syntax: None  |  size: 1.02 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Store Type in field/variable
  2. public class Logger
  3. {
  4.     public static Type Writer;
  5.  
  6.     public static void SetWriter(Type @new)
  7.     {
  8.         Writer = @new;
  9.     }
  10.  
  11.     public static void Write(string str)
  12.     {
  13.         Writer.Write(str);
  14.     }
  15. }
  16.        
  17. Writer.Write(str);
  18.        
  19. public interface IWriter
  20. {
  21.     public Write(string text);
  22. }
  23.  
  24. public class Logger
  25. {
  26.     public static IWriter Writer;
  27.  
  28.     public static void SetWriter(IWriter newWriter)
  29.     {
  30.         Writer = newWriter;
  31.     }
  32.  
  33.     public static void Write(string str)
  34.     {
  35.         Writer.Write(str);
  36.     }
  37. }
  38.        
  39. public class MyWriter : IWriter
  40. {
  41.     public void Write(string text)
  42.     {
  43.         // Do something to "write" text
  44.     }
  45. }
  46.  
  47. Logger.SetWriter(new MyWriter());
  48.        
  49. Type variableName = typeof(SomeTypeName);
  50.        
  51. Type variableName = someObject.GetType();
  52.        
  53. public class Logger
  54. {
  55.     public Logger(TextWriter writer)
  56.     {
  57.         _writer = writer;
  58.     }
  59.  
  60.     private TextWriter _writer;    
  61.  
  62.     public void Write(string text)
  63.     {
  64.         _writer.Write(text);
  65.     }
  66. }