Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8145521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:41:18+00:00 2026-06-06T13:41:18+00:00

[Serializable] public class SampleBase { [HumanReadableAttribute(The Base)] public string BaseProp { get; set; }

  • 0
[Serializable]
public class SampleBase
{
    [HumanReadableAttribute("The Base")]
    public string BaseProp { get; set; } // "testing things"
    public bool AllUrBase { get; set; } // true

    public string AsHumanReadable()
    {
        var type = GetType();
        var properties = type.GetProperties();
        var stringRepresentation = new List<string>();
        foreach (var p in properties)
        {
            var ca = p.GetCustomAttributes(true).FirstOrDefault(a => a is HumanReadableAttribute);
            var v = p.GetValue(this, null);
            var t = v.GetType();
            var e = t.IsEnum ? t.GetField(Enum.GetName(t, v)).GetCustomAttributes(typeof(HumanReadableAttribute), false).FirstOrDefault() as HumanReadableAttribute : null;
            stringRepresentation.Add(string.Format("{0}: {1}", ca ?? p.Name, v is bool ? ((bool) v ? "Yes" : "No") : e ?? v));
        }
        return string.Join("\r\n", stringRepresentation);
    }
}

[Serializable]
public class SampleClass {
    public enum MyEnum {
        [HumanReadableAttribute("It makes sense")]
        MakesSense,
        [HumanReadableAttribute("It's weird")]
        Nonsense
    }
    [HumanReadableAttribute("What do you think about this")]
    public MyEnum MyProperty { get; set; } // Nonsense
}

Calling the AsHumanReadable method will produce

The Base: testing things
AllUrBase: Yes
What do you think about this: It's weird

My users may add SampleClass objects (or a wide range of other objects implementing SampleBase) to a shopping cart which will stored for later use.
A customer care worker will be reading the output for checking up on things.

I’ve thought about making ToString methods (like return "The Base" + BaseProp;) instead. My frist thought is that the attribute approach is the more flexible one.

What are the pros and cons to those two approaches (maintainance, localization (avoid magic strings when possible) etc.)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:41:20+00:00Added an answer on June 6, 2026 at 1:41 pm

    I recommend dropping the custom implementation and having a look at ServiceStack.Text, in particular at the T.Dump methods.

    It’s something already implemented and reliable. The only thing that it doesn’t cover are friendly boolean strings (Yes/No)/.

    A custom implementation involves first creating a good and working solution, which takes time and then maintenance and adding support for new stuff, which also takes time.
    There are many ways through which you can do it, with attributes being one of them, but it’s simply not worth it when you have ServiceStack.

    Ultimately, you could modify the T.Dump methods and customize them to fit your requirements. It’s still something not from scratch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[Serializable] public class XX { [XmlAttribute(name)] public string name{get;set;} } [Serializable] [XmlRoot(tree)] public class
[Serializable] public class KeyValue { public List<Dictionary<string, string>> pair { get; set; } }
[Serializable] public class appData { public string DataFile { get; set; } public string
I've a simple class [Serializable] public class MyClass { public String FirstName { get;
@Entity public class TestClass implements Serializable{ private Integer id; private Set<String> mySet; @Id @GeneratedValue
Consider the following code: [Serializable] public class Human { public string Name { get;
My class [Serializable] public class VSpecialQualifiers { public VCD LivingStatus { get; set; }
[Serializable] public class ModelResource:ISerializable { public Int64 Ore { get; private set; } public
Consider the following code: [Serializable] public class Region { public double North {get; set;}
I have a webservice [Serializable] public class DataObject { public int ID { get;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.