
Untitled
By: a guest on
Aug 5th, 2012 | syntax:
None | size: 1.02 KB | hits: 5 | expires: Never
Store Type in field/variable
public class Logger
{
public static Type Writer;
public static void SetWriter(Type @new)
{
Writer = @new;
}
public static void Write(string str)
{
Writer.Write(str);
}
}
Writer.Write(str);
public interface IWriter
{
public Write(string text);
}
public class Logger
{
public static IWriter Writer;
public static void SetWriter(IWriter newWriter)
{
Writer = newWriter;
}
public static void Write(string str)
{
Writer.Write(str);
}
}
public class MyWriter : IWriter
{
public void Write(string text)
{
// Do something to "write" text
}
}
Logger.SetWriter(new MyWriter());
Type variableName = typeof(SomeTypeName);
Type variableName = someObject.GetType();
public class Logger
{
public Logger(TextWriter writer)
{
_writer = writer;
}
private TextWriter _writer;
public void Write(string text)
{
_writer.Write(text);
}
}