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 6766883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:53:05+00:00 2026-05-26T14:53:05+00:00

Unfortunately I am still working on .NET 2.0. I have not created a custom

  • 0

Unfortunately I am still working on .NET 2.0. I have not created a custom attribute before.
I want to create a CustomStringFormatAttribute:.

If a class, say Customer.Name, has:

MaxLength=30
ActualLength=10

I need to pad it with empty spaces till it reached 30.

I also need an attribute for date that I can format like DisplayDataFormat

I have created the following but How do I get access to the actual value of the property within the attribute?

public class Customer
{
    [CustomStringFormatAttribute(30)]
    public string Name { get; set; }

    //todo:customDateAttribute
    public DateTime StartDate { get; set; }
}

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
public sealed class CustomStringFormatAttribute : Attribute
{
    private readonly int maxLength;

    public CustomStringFormatAttribute(int maxLength)
    {
       MaxLength = maxLength;
    }

    public  int MaxLength { get; private set; }

    //?Should I override ToString 
    public override string ToString()
    {
        return Format();
    }

    private string Format()
    {
        //simplified version of my formatting for brevity
        string source = "value from the property of the class.";//How do I get access to the actual value of the property within the attribute?
        const char paddingChar = ' ';
        return source.PadLeft(maxLength, paddingChar);
    }    
}

Any suggestions?

Note: I’ used automatic property for brevity. I don’t have that luxury in .NET 2.0.

  • 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-05-26T14:53:06+00:00Added an answer on May 26, 2026 at 2:53 pm

    Sorry, you cannot access the class instance or the property info inside your attribute.
    You should write an additional method, for example a static method in some “static” class, that allow you to do what you want to do.

    Example….

        public static string FormatProperty(object instance, PropertyInfo property)
        {
            CustomStringFormatAttribute attrib = Attribute.GetCustomAttribute(property, typeof(CustomStringFormatAttribute)) as CustomStringFormatAttribute;
            return property.GetValue(instance, null).ToString().PadLeft(attrib.MaxLength, ' ');
        }
    
        public static string FormatProperty(object instance, string propertyName)
        {
            return FormatProperty(instance, instance.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance));
        }
    

    But this is very uncomfortable and insanely slow since uses reflection to get property value through property info.

    To access property attributes you need a PropertyInfo.

        public static int GetPropertyMaxLength(PropertyInfo property)
        {
            CustomStringFormatAttribute attrib = Attribute.GetCustomAttribute(property, typeof(CustomStringFormatAttribute)) as CustomStringFormatAttribute;
            return attrib != null ? attrib.MaxLength : int.MaxValue;
        }
    
        public static int GetPropertyMaxLength(Type type, string propertyName)
        {
            return GetPropertyMaxLength(type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance));
        }
    

    Let’s suppose we put these functions inside the attribute.
    Then we want to override, for example, our ToString method in our class Customer.

    public override string ToString()
    {
        return CustomStringFormatAttribute.FormatProperty(this, "Name");
    }
    

    The problem with this is of course Speed, it uses reflection by name, very slow, and Refactoring, you will not have a compile time warning or error if property “Name” doesn’t exists, you will get only an exception at runtime. I would suggest you to use another mechanism.

    With newer version of the language you could use lambda expressions to obtain your property info directly by the property itself, but since you are in C# 2.0 this is not possible.

    Another solution can be: add instead another property called FormattedXXX, for example FormattedName that returns the lenght as you want it and you can use that property instead of the Name property.
    This will allow you to keep your formatted version of the property near your property.

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

Sidebar

Related Questions

I have created a custom labeller for CC.Net which is working almost perfectly, however
I have some terribly written ASP.NET code that is just not working right (go
I'm trying to use eclipse encoding UTF-8, for polish characters. Unfortunately, I still have
I read some information about named branches and working with bookmarks. Unfortunately I still
I've seen various other posts about this, but unfortunately I still haven't been able
Unfortunately, I have to deal with J2ME (which I consider ancient technology these days)
Unfortunately, I have to use a C library with internal state in my Android
Unfortunately I have a pretty bad understanding of how to properly set up threading.
Unfortunately their Wiki is down for maintenance and the web is not being helpful.
Unfortunately I haven't coded Java for about five years and I absolutely can not

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.