I want to load a properties file (it\'s a .csv file having on each line a name and associated numeric value) and then access those property values like so: FileLoader
Without having your "FileLoader" actually rewrite itself, and then using Reflection to access the newly created properties ther's not really any way to do this. (Completley ignore this answer if you're after something that works at Design/Compile time, rather than at runtime =)
What you'll probably end up doing is having something like
public class FileLoader
{
// ... Other code for FileLoader goes here
public FileLoader(string propertiesFileNameAndPath)
{
// Load values from propertiesFile into _properties / _propertyValues
}
private _properties = new List();
private _propertyValues = new Dictionary();
public string[] Properties
{
// returning as an array stops your _properties being modified
return _properties.ToArray();
}
public void UpdateProperty(string propertyName, string propertyValue)
{
if (propertyValues.ContainsKey(propertyName))
{
propertyValues[propertyName] = propertyValue;
}
}
public string GetPropertyValue(string propertyValue)
{
if (propertyValues.ContainsKey(propertyName))
{
return propertyValues[propertyName];
}
}
}
Now you can do:
var fileLoader = new FileLoader("path to properties.csv");
foreach(var property in fileLoader.Properties)
{
var propertyValue = fileLoader.GetPropertyValue(property);
}
Of course you could just simplify it down to loading it into a dictionary that you return from a method on FileLoader, but the code above maintains part of the "appearance" of using properties of the FileLoader class =)
Edit: Add "indexer" code
One thing that might make the syntax cleaner would be to use an "Indexer", so you'd add the following to FileLoader:
public string this[string index]
{
if (propertyValues.ContainsKey(propertyName))
{
return propertyValues[propertyName];
}
else
{
return null;
}
}
Then the code for accessint it would be the slightly tidier:
var fileLoader = new FileLoader("path to properties.csv");
foreach(var property in fileLoader.Properties)
{
var propertyValue = fileLoader[property];
}