The title is a bit misleading, I am actually trying to learn how to use Castle Windsor and I think I understand registering the interfaces/objects.
What I can't seem to grasp is how to set properties dynamically at Runtime?
Below is my code, an IFileWriter interface implemented by the TextFileWriter concrete class.
Then I have the code for my test application to I've registered the interface and the object.
Now - how do I instantiate the TextFileWriter and set the FilePath, OutputFileName, and DataToWrite properties?
namespace IOCv2Library.Interfaces
{
public interface IFileWriter
{
string OutputFileName { get; set; }
string filePath { get; set; }
DataTable DataToWrite { get; set; }
void WriteFile();
}
}
namespace IOCv2Library
{
public class TextFileWriter : IFileWriter
{
private DataTable _dataTable = null;
private string _filePath = String.Empty;
private string _outputFileName = String.Empty;
public DataTable DataToWrite
{
get{ return _dataTable; }
set { _dataTable = value; }
}
public string filePath
{
get { return _filePath; }
set { _filePath = value; }
}
public string OutputFileName
{
get { return _outputFileName; }
set { _outputFileName = value; }
}
public void WriteFile()
{
string fullFileName = Path.Combine(new string[] { _filePath, _outputFileName });
using (StreamWriter file =
new StreamWriter(fullFileName, true))
{
foreach (DataRow drRow in _dataTable.Rows)
{
foreach (DataColumn dcCol in _dataTable.Columns)
{
file.Write(drRow[dcCol].ToString() + "\t");
}
file.Write(Environment.NewLine);
}
}
}
}
}
namespace TestCastleWindsorIOCv2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var container = new WindsorContainer();
container.Register(Component.For<IFileWriter>().ImplementedBy<IOCv2Library.TextFileWriter>());
}
}
}