Click here to Skip to main content
11,705,610 members (47,654 online)
Click here to Skip to main content

Tagged as

Useful wrapper class to override the Object.ToString method

, 3 Oct 2011 CPOL 27.7K 9
Rate this:
Please Sign up or sign in to vote.
A useful wrapper class to override the Object.ToString method.

This class can be useful for when you want to override the Object.ToString method on a type you don't have access to edit.

public class ToStringWrapper<T>
{
    private readonly T _wrappedObject;
    private readonly Func<T, string> _toStringFunction;
 
    public T WrappedObject
    {
        get { return _wrappedObject; }
    }
 
    public ToStringWrapper(T wrappedObject, Func<T, string> toStringFunction)
    {
        this._wrappedObject = wrappedObject;
        this._toStringFunction = toStringFunction;
    }
 
    public override string ToString()
    {
        return _toStringFunction(_wrappedObject);
    }
}

Sample usage: Don't have access to edit the Person type and you want the Name property's value to be returned in the ToString method.


public sealed class Person
{
    public string Name { get; set; }
}
 
class Program
{
    static void Main(string[] args)
    {
        // Person type not wrapped.  Type will be returned in ToString method
        var person = new Person { Name = "TestName" };
        Console.WriteLine(person);
 
        // Person type wrapped with ToStringWrapper<T> class and supplying
        // a function that will return the Person class's Name property value.
        var personWrapper = new ToStringWrapper<Person>(person, i => i.Name);
        Console.WriteLine(personWrapper);
 
        Console.ReadKey();
    }
}

Output:
ConsoleApplication1.Person
TestName

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jay_Cee
South Africa South Africa
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 5 Also usefull in the debugger :) Pin
Eddy Vluggen13-Sep-11 13:14
memberEddy Vluggen13-Sep-11 13:14 
GeneralRe: I think DebuggerDisplay and DebuggerTypeProxy attributes are... Pin
moozzyk10-Oct-11 19:57
membermoozzyk10-Oct-11 19:57 
GeneralReason for my vote of 5 Nice Pin
Shahare12-Sep-11 19:32
memberShahare12-Sep-11 19:32 
Reason for my vote of 5
Nice
GeneralI concur the idea is nice. I find Edward Nutting's comment v... Pin
Ilka Guigova5-Sep-11 16:05
memberIlka Guigova5-Sep-11 16:05 
GeneralI would say that instead of having to create a new ToStringW... Pin
Edward Nutting4-Sep-11 5:31
memberEdward Nutting4-Sep-11 5:31 
Generalyou appear to be missing a <T> from your class declaration a... Pin
GParkings2-Sep-11 11:50
memberGParkings2-Sep-11 11:50 
GeneralRe: Thanks, had some formatting issues which should be fixed now... Pin
Jay_Cee4-Sep-11 22:02
memberJay_Cee4-Sep-11 22:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 3 Oct 2011
Article Copyright 2011 by Jay_Cee
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid