none
Dynamic String Formatting == Dynamic Assemblies ? RRS feed

  • Question

  • My users need to be able to specify their own formatting preferences for certain strings in my system for display on forms, reports, etc.

    Here's an extremely simplified example of what I'm talking about.

    In the app Settings or the database or wherever, they specify a format string: "%lastName% can be reaced at %mainPhone% or %email%" to control the output of this class:

    class Person
    {
    string nameFirst;
    string nameLast;
    string eMail;
    List<Phone> phones;
    }
    I'm wondering how to best produce the final string at runtime in a reasonably efficient manor. It's doubtful there will be more than a few hundred of these items at a time (less than a dozen most likely) and this string will be produced for tooltips, columns in grids, reports, etc. Still - while I don't have to worry about this logic being executed a 1,000 times a second - I don't want a slow UI.

    It seems to me this is a good use for codeDOM and generating a dynamic method. When the app is loaded, it reads the configuration string and generates a method that looks

    string DisplayAs()
    {
    // i.e. based on the configuration string originally specified as "%lastName% can be reaced at %mainPhone% or %email%"
    string fmt = "{0} can be reached at {1} or {2}";

    string[] args = { nameLast, SomeMethodThatKnowsHowToDetermin_MAIN_PhoneFromList(), eMail };

    return string.Format ( fmt, args );
    }
    I've never used the codeDOM but I think I can manage something like this. But is this overkill or maybe there's a better way?

    I could take a brute force approach and loop through whatever format string the user supplies doing string substitutions based on my list of 'keywords' (i.e. %mainPhone%) but there could easily be a dozen or so of those keywords. Doing that in every ToString() override just seems painfully inefficient to me - but maybe not to the extent I should worry about it.

    I looked at IFormatting but it's not applicable in this context.

    Thanks,
    Dan Holt
    Thursday, July 3, 2008 2:01 PM

Answers

All replies