tags:

views:

154

answers:

2

I have this class/object below:

public class Person
{
  public  string FirstName;
  public  string MI;
  public  string LastName;
}

Person p=new Person();
      p.FirstName = "Jeff";
      p.MI = "A";
      p.LastName = "Price";

Is there any built in in linq or c# or in subsonic that will create an output of this?:

string myString = "FirstName=\"Jeff\" p.MI=\"A\" p.LastName=\"Price\"";
+2  A: 

It seems you need a ToString overload in Person. Also, don't expose public fields like that. Use properties.

    public class Person
    {
        public string FirstName { get; set; }
        public string MI { get; set; }
        public string LastName { get; set; }

        public override string ToString()
        {
            return "FirstName=\"" + FirstName + "\" p.MI=\"" + MI + "\" p.LastName=\"" + LastName + "\"";
        }
    }

(edit)

Here's your request (but it requires properties):

    public static class ObjectPrettyPrint
    {
        public static string ToString(object obj)
        {
            Type type = obj.GetType();
            PropertyInfo[] props = type.GetProperties();
            StringBuilder sb = new StringBuilder();
            foreach (var prop in props)
            {
                sb.Append(prop.Name);
                sb.Append("=\"");
                sb.Append(prop.GetValue(obj, null));
                sb.Append("\" ");
            }

            return sb.ToString();
        }
    }

Usage:

        Console.WriteLine(ObjectPrettyPrint.ToString(new Person { FirstName, = "A", MI = "B", LastName = "C"  }));
bruno conde
Nice one @bruno, but still waiting for more inputs
No Body
See the updated answer.
bruno conde
yup, now trying...
No Body
you nailed it man!!!Id="6" UserId="xxx3" SurName="NoBodyx" FirstName="no3" MiddleName="B." Email="[email protected]" MobilePhone="mobile3" Phone="phone3" IsITIAdmin="False" Password="tHgL5AHoDasddfCmUt0234ffcOuQ" ITIStatus="Not Accepted" ITInstructionID="1" ITIPublishedDate="8/5/2009 12:00:00 AM" ITIDueDate="8/14/2009 12:00:00 AM" ITIAcceptedDate="" CreatedDate="8/6/2009 1:57:58 PM" CreatedBy="ca3" UpdatedDate="8/7/2009 5:58:37 AM" UpdatedBy="qqq" Name="no3 B. NoBodyx" UrlLink="http://www.google.com?p=LLw3ZYXIaEq0BEJM0kjZhg%3d%3d"
No Body
What [attribute] should i use in order to skip certain property by the reflection?
No Body
Nice piece to implement as extension.
Dykam
@No Body, To ignore some properties either you can change all the types that use this and implement a custom attribute that will decorate the properties to ignore or pass a list of properties to ignore in the method itself (or some predicate that validates a property). Both require changes in this method.
bruno conde
thanks @Dykam you got it
No Body
+1  A: 

Well, as for LINQ and C#, not by default.

However, in the Person class you can override the ToString() event to do it for you.

    public override string ToString()
    {
        return string.Format("p.Firstname={0} p.MI={1} p.LastName={2}", FirstName, MI, LastName);
    }

And then you would just call it as follows:

string myString = p.ToString();

Which will give you the output you are looking for.

Kyle Rozendo
I am a lazy guy, same code as Bruno above. Am looking now for some reflection
No Body
Reflection? Why in the world would you want to do that if all this takes is a simple override?
Kyle Rozendo
I am just pushing the limit here, well your and Brunos idea are simple and straight forward that's why I am applying it already while waiting for some more answer =)
No Body
I then recommend changing your question a bit. Say that you don't want to override and somehow work in the whole "pushing the limit" thing. I still cannot understand it however, but hey, if that's what you want to do.. ;)
Kyle Rozendo
Thanks, because I got around closed to 80 objects =(
No Body