tags:

views:

145

answers:

7

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.PropertyOne or FileLoader.PropertyTwo. The problem is I don't want to have to write a property for each value, I want them to be generated from the file. So

public class FileLoader
{
    public int Property1 { get; private set; }
}

is not what I'm looking for. Is this possible? I can't see any way to do it because obviously the compiler wouldn't know about the property names. Perhaps something similar?

Update: thanks to all for their answers. Very illuminating.

+1  A: 

You'll basically need to do some code generation.

Write a simple console app, or win forms app, that loads the csv, then takes the information from the csv and generates .cs files.

Jack Marchetti
I think he wants them to be generated at run-time, rather than compile time, although generating code through a macro (or script or whatever) is still a good route. Run-time generation might be necessary if the properties are changing while the app is running.
FrustratedWithFormsDesigner
I think it's about compile-time, because otherwise it wouldn't really make sense to have real properties and *not having to write the declarations by hand*. However.
OregonGhost
+2  A: 

Code generation like this can be done in several different ways.

  • Visual Studio has T4 templates.
  • You can use external tools like My Generation (it is geared towards ORM code generations, not sure it supports CSV).
  • Or roll out your own code to read the file and write out such classes (to be created with a .generated.cs suffix).
Oded
+6  A: 

In C#4.0, you could use the ExpandoObject, link contains good explanation and a couple of use cases, like :

dynamic contact = new ExpandoObject();
contact.Name = "Patrick Hines";
contact.Phone = "206-555-0144";
contact.Address = new ExpandoObject();
contact.Address.Street = "123 Main St";
contact.Address.City = "Mercer Island";
contact.Address.State = "WA";
contact.Address.Postal = "68402";

Though the awesomeness of the ExpandoObject is to dynamicly create complex hierarchical objects, I suppose you could use it in general for it's shiny sintax even for simple dynamicly defined objects.

EDIT: Here is another answer on SO adding details about the benefits of ExpandoObject by the same columnist that wrote the previously linked article

What are the true benefits of ExpandoObject?

Dynami Le Savard
In my opinion, this is one of the cases that illustrate why dynamic is a bad idea. No Intellisense, no type safety, no compile-time checks, runtime exceptions - I think code generation would be a whole lot better.
OregonGhost
A: 

This is possible with the new dynamic stuff in c# 4.0. I'm not an expert, but with dynamic objects you can define behavior for when a method that does not exist is evoked. This post shows a pretty fun example of how to set up a dynamic dictionary that could do what you want.

CaptnCraig
+1  A: 

There's an easy solution for that, read the properties into a dictionary and overload the FileLoader's array operator:

public T this[string propertyName]  
{  
    get { return yourDictionary[propertyName]; }  
}

That way, you can access the properties using fileLoadObject["SomePropertyName"].

As Oded pointed out, it is possible to dynamically add properties with Reflection. There's an example over here.

AndiDog
What about Reflection? You can create dynamic objects with it.
Oded
Thanks for the hint, I didn't know about that.
AndiDog
@AndiDog - you can use reflection to create properties in C#. I have some example code somewhere that I've used it on, just have to dig it up
Cody C
Ok, I edited the answer to provide the correct infos. I also added a tutorial for creating properties dynamically.
AndiDog
Can you tell me where it is?
Johnny
??? It's in my answer ("There's an example over here." <-- click on the link).
AndiDog
+2  A: 

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<string>();
  private _propertyValues = new Dictionary<string, string>();

  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];
}
Rob
+1 for indexer. That's what I thought straight away when reading the question.
Wim Hollebrandse
A: 

Here's a sample using ExpandoObject and C#`s 4.0 dynamic feature

public dynamic ParseCsvFile(string filePath) {
  var expando = new ExpandoObject;
  IDictionary<string,object> map = expando;
  foreach ( var line in File.ReadAllLines(filePath)) {
    var array = line.Split(new char[]{','},2);
    map.Add(array[0],array[1]);
  }
  return expando;
}

...
var d = ParseCsvFile(someFilePath);
Console.WriteLine(d.Property1);
JaredPar