Johnny
Johnny

Reputation: 7351

Generate properties programmatically

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?

Upvotes: 11

Views: 12012

Answers (7)

AndiDog
AndiDog

Reputation: 70218

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.

Upvotes: 1

Dynami Le Savard
Dynami Le Savard

Reputation: 5036

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 dynamically create complex hierarchical objects, I suppose you could use it in general for it's shiny syntax even for simple dynamically 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?

Upvotes: 15

JaredPar
JaredPar

Reputation: 755337

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);

Upvotes: 2

Rob
Rob

Reputation: 45789

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];
}

Upvotes: 2

captncraig
captncraig

Reputation: 23118

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.

Upvotes: 0

Oded
Oded

Reputation: 499302

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).

Upvotes: 2

Jack Marchetti
Jack Marchetti

Reputation: 15754

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.

Upvotes: 1

Related Questions