Generate properties programmatically

前端 未结 7 688
难免孤独
难免孤独 2021-01-04 17:47

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

相关标签:
7条回答
  • 2021-01-04 18:28

    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.

    0 讨论(0)
  • 2021-01-04 18:32

    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];
    }
    
    0 讨论(0)
  • 2021-01-04 18:40

    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.

    0 讨论(0)
  • 2021-01-04 18:42

    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?

    0 讨论(0)
  • 2021-01-04 18:46

    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).
    0 讨论(0)
  • 2021-01-04 18:48

    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);
    
    0 讨论(0)
提交回复
热议问题