views:

704

answers:

7

I think the direct answer to the question is 'No' but I'm hoping that someone has written a real simple library to do this (or I can do it...ugh...)

Let me demonstrate what I am looking for with an example. Suppose I had the following:

class Person {
  string Name {get; set;}
  int NumberOfCats {get; set;}
  DateTime TimeTheyWillDie {get; set;}
}

I would like to be able to do something like this:

static void Main() {
  var p1 = new Person() {Name="John", NumberOfCats=0, TimeTheyWillDie=DateTime.Today};
  var p2 = new Person() {Name="Mary", NumberOfCats=50, TimeTheyWIllDie=DateTime.Max};

  var str = String.Format(

"{0:Name} has {0:NumberOfCats} cats and {1:Name} has {1:NumberOfCats} cats.  They will die {0:TimeTheyWillDie} and {1:TimeTheyWillDie} respectively
", p1, p2);

  Console.WriteLine(str);
}

Does anyone know if theres a format for doing something like this or if someone has written a library to do it? I know it shouldn't be too hard, but I'd rather not be reimplementing the wheel.

+4  A: 

You could override the ToString() for your class.

Good Article here

KiwiBastard
It won't help, since he's combining more than one instance in the same string. Also: do you really want to do that, or could there ever be another .ToString() interpretation you might want?
Joel Coehoorn
Ah I see what you're saying. Thats awful messy though. And it is a rather limited method. Would be easier to write your own String.Format
George Mauer
Yeah more useful if you want a way of exposing multiple properties in a string. Especially using the IFormattable interface.
KiwiBastard
+3  A: 

What is after the ":" is passed as an argument to the ToString method of your class.
Just declare a ToString method accepting a string, and the 'Name', 'NumberOfCats' etc. will be passed in that parameter.

EDIT: You must implement System.IFormattable. This works:

class Person : IFormattable
{
    public override string ToString()
    {
        return "Person";
    }

    public string ToString(string format, IFormatProvider formatProvider)
    {
        if (format == "Name")
        {
            return "John";
        }
        if (format == "NumberOfCats")
        {
            return "12";
        }
        return "Unknown format string";
    }

}

class Program
{
    static void Main(string[] args)
    {
        Person p = new Person();
        Console.WriteLine(string.Format("Name = {0:Name}",p));
        Console.WriteLine(string.Format("NumberOfCats = {0:NumberOfCats}", p));
    }
}
Paolo Tedesco
I don't think this is true, ToString() doesn't take any arguments
George Mauer
It's a good thought! There are overloads of ToString on many .NET objects that take a string or an IFormatProvider as arguments, but unfortunately as George Mauer pointed out these do not exist on Object.
Barry Fandango
Cool. Thanks for sharing.
Guge
That's one heck of an edit. But yeah, if that's what you meant thats actually a pretty darn good idea...
George Mauer
No, it's not what I meant, I just had got it wrong in the first instance :)It's quite a lot of time I haven't been programming in C#.
Paolo Tedesco
+1  A: 

I really don't see how this:

"{0:Name} has {0:NumberOfCats} cats and {1:Name} has {1:NumberOfCats} cats.  They will die {0:TimeTheyWillDie} and {1:TimeTheyWillDie} respectively", p1, p2);

is any better than this:

"{0} has {1} cats and {2} has {3} cats.  They will die {4} and {5} respectively
", p1.Name, p1.NumberOfCats, p2.Name, p2.NumberOfCats, p1.TimeTheyWillDie, p2.TimeTheyWillDie);

In fact, since you're losing intellisense help in the first one, not only is it more prone to failure but would probably take longer to write in an IDE.

And if you wanted to do something like this, you can always whip up an extension method for it. I bet it would look like a nightmare, tho.

Will
In my pov String.Format is a sort of simple view engine, why should it be any more limiting? If you have a stable domain model, there should be no problem with defining the exact format of output strings in a configuration. It would be rather convenient.
George Mauer
A: 

This is something that many do in the Python world by using "someString % locals()." What you're suggesting though has a couple of fundamental breaks from how string.format works:

  • normally the placeholder notation has string formatting information after the colon, whereas you want to do property access.

  • the placeholder indices ({0, {1, etc) normally refer to the numberes arguments in the params args, but it appears you would like your function to render the whole string for each parameter that is passed in. Should they be concatenated? returned as a string array?

So you may end up writing this one yourself, in which case you can skip the index notation entirely (so {NumberOfCats} instead of {0:NumberOfCats} or even use property name followed by format provider {NumberOfCats:c}. Consuming the metadata from an input object shouldn't be too tough.

Barry Fandango
Not so on point two. {0:NumberOfCats} would denote p1.NumberOfCats where p1 is the first positional argument. Good catch on the first point though, it would indeed need some additional syntax
George Mauer
A: 

Boo or Nemerle has something like this. I have tried to think of a nice simple way to do it for a couple of years now, with no easy answer.

The best you can do is provide you own IFormatProvider, and parse the input manually (the crappy part...).

leppie
A: 

If you decide to parse the format string yourself, you should consider this...:

The Spring.NET project has the Spring.NET Expression Language in Spring.Core. It lets you query an object graph by pointing to properties using strings. Using your example, you could imagine something like this:

var person = new Person { Name = "joe", Email = new Email { Address = "[email protected]" } };

var message = string.Format("{0}'s e-mail is {1}",
    ExpressionEvaluator.GetValue(person, "Name"), 
    ExpressionEvaluator.GetValue(person, "Email.Address"));

(i would probably not store an e-mail like that, but I could not come up with anything better)

mookid8000
+4  A: 

Edit: You don't have to implement IFormattable for each object...that'd be a PITA, severely limiting, and a fairly large maintenance burden. Just use Reflection and a IFormatProvider with ICustomFormatter and it'll work with any object. String.Format has an overload to take one as a parameter.

I've never thought of this before, but you intrigued me - so I had to give it a quick whirl. Note that I chose to allow an additional format string to be passed to the property value, and that it only works with non indexed and accessible properties (though you could easily add that).

public class ReflectionFormatProvider : IFormatProvider, ICustomFormatter {
    public object GetFormat(Type formatType) {
        return formatType == typeof(ICustomFormatter) ? this : null;
    }

    public string Format(string format, object arg, IFormatProvider formatProvider) {
        string[] formats = (format ?? string.Empty).Split(new char[] { ':' }, 2);
        string propertyName = formats[0].TrimEnd('}');
        string suffix = formats[0].Substring(propertyName.Length);
        string propertyFormat = formats.Length > 1 ? formats[1] : null;

        PropertyInfo pi = arg.GetType().GetProperty(propertyName);
        if (pi == null || pi.GetGetMethod() == null) {
            // Pass thru
            return (arg is IFormattable) ? 
                ((IFormattable)arg).ToString(format, formatProvider) 
                : arg.ToString();
        }

        object value = pi.GetGetMethod().Invoke(arg, null);
        return (propertyFormat == null) ? 
            (value ?? string.Empty).ToString() + suffix
            : string.Format("{0:" + propertyFormat + "}", value);
    }
}

And your slightly modified example:

var p1 = new Person() {Name="John", NumberOfCats=0, TimeTheyWillDie=DateTime.Today};
var p2 = new Person() {Name="Mary", NumberOfCats=50, TimeTheyWillDie=DateTime.MaxValue};

var str = string.Format(
    new ReflectionFormatProvider(),
    @"{0:Name} has {0:NumberOfCats} cats and {1:Name} has {1:NumberOfCats} cats. 
    They will die {0:TimeTheyWillDie:MM/dd/yyyy} and {1:TimeTheyWillDie} respectively.
    This is a currency: {2:c2}.", 
    p1, 
    p2,
    8.50M
);

Console.WriteLine(str);

Outputs:

John has 0 cats and Mary has 50 cats. 
They will die 12/10/2008 and 12/31/9999 11:59:59 PM respectively.
This is a currency: $8.50.
Mark Brackett
Wow Mark. Very slick...very slick indeed
George Mauer