How do I automatically display all properties of a class and their values in a string? [duplicate]

I think you can use a little reflection here. Take a look at Type.GetProperties(). public override string ToString() { return GetType().GetProperties() .Select(info => (info.Name, Value: info.GetValue(this, null) ?? “(null)”)) .Aggregate( new StringBuilder(), (sb, pair) => sb.AppendLine($”{pair.Name}: {pair.Value}”), sb => sb.ToString()); }

Is specialization of std::to_string for custom types allowed by the C++ standard?

In C++11 and later, is it allowed to specialize std::to_string in the std namespace for custom types? No. First of all, it is not a template function so you can’t specialize it at all. If you’re asking about adding your own overload functions the answer still remains the same. Documentation snippet from Extending the namespace … Read more

.NET: How to convert Exception to string?

ErrorCode is specific to ExternalException, not Exception and LineNumber and Number are specific to SqlException, not Exception. Therefore, the only way to get these properties from a general extension method on Exception is to use reflection to iterate over all of the public properties. So you’ll have to say something like: public static string GetExceptionDetails(this … Read more

Using Google Guava’s Objects.ToStringHelper

I have a little trick for Guava’s com.google.common.base.MoreObjects.toStringHelper(). I configured IntelliJ IDEA to use it when auto-generating toString() methods. I assume you can do the same in Eclipse. Here’s how to do it in Intellij: go inside a class hit Alt + Insert to popup the “Generate” menu choose toString() click the “Settings” button go … Read more

toString(), equals(), and hashCode() in an interface

It sounds like you want to force your classes to override the default implementations of those methods. If so, the way to do this is to declare an abstract superclass that has the methods declared as abstract. For example: public abstract class MyBaseClass implements … /* existing interface(s) */ { public abstract boolean equals(Object other); … Read more

How to create an extension method for ToString?

Extension methods are only checked if there are no applicable candidate methods that match. In the case of a call to ToString() there will always be an applicable candidate method, namely, the ToString() on object. The purpose of extension methods is to extend the set of methods available on a type, not to override existing … Read more

Dumping a java object’s properties

You could try XStream. XStream xstream = new XStream(new Sun14ReflectionProvider( new FieldDictionary(new ImmutableFieldKeySorter())), new DomDriver(“utf-8”)); System.out.println(xstream.toXML(new Outer())); prints out: <foo.ToString_-Outer> <intValue>5</intValue> <innerValue> <stringValue>foo</stringValue> </innerValue> </foo.ToString_-Outer> You could also output in JSON And be careful of circular references 😉

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)