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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:55:23+00:00 2026-05-20T10:55:23+00:00

I have an attribute whose constructor takes two strings. I would like to make

  • 0

I have an attribute whose constructor takes two strings. I would like to make the second string an optional parameter. If the user of the attribute does not supply a value for the second parameter, its value should be equal to the name of the field possesing the attribute. How could I achieve that?


Here is my code:

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class WSFieldAttribute : Attribute
{
    private string tableField;
    private string xmlField;

    public WSFieldAttribute(string tableField, string xmlField)
    {
        this.tableField = tableField;
        this.xmlField   = xmlField;
    }

    public object this[DataRow row]
    {
        get { return row[this.tableField]; }
    }

    public object this[XmlWriter writer]
    {
        set
        {
            string toString;

            if (value.GetType() == typeof(string))
                toString = (string)value;
            else if (value.GetType() == typeof(DateTime))
                toString = Utilities.FormatDate((DateTime)value);
            else if (value.GetType() == typeof(DateTime?))
                toString = Utilities.FormatDate((DateTime?)value);
            else
                toString = value.ToString();

            writer.WriteAttributeString(this.xmlField, toString);
        }
    }
}

public class FieldDefinition
{
    private FieldInfo        fieldInfo;
    private WSFieldAttribute wsField;

    public FieldDefinition(FieldInfo fieldInfo)
    {
        object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(WSFieldAttribute), false);

        if (customAttributes.Length != 1)
            throw new InvalidProgramException();

        this.fieldInfo = fieldInfo;
        this.wsField = (WSFieldAttribute)customAttributes[0];
    }

    public object this[DataRow row]
    {
        get { return wsField[row]; }
    }

    public string this[XmlWriter writer]
    {
        set { wsField[writer] = value; }
    }

    public void Read(object element, DataRow row)
    {
        fieldInfo.SetValue(element, this.wsField[row]);
    }

    public void Write(object element, XmlWriter writer)
    {
        this.wsField[writer] = fieldInfo.GetValue(element);
    }
}

public class Row<T> : IXmlSerializable where T : class, new()
{
    private static Type type;
    private static LinkedList<FieldDefinition> fieldDefinitions;

    static Row()
    {
        type = typeof(T);

        FieldInfo[] fieldInfos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

        foreach (FieldInfo fieldInfo in fieldInfos)
            fieldDefinitions.AddLast(new FieldDefinition(fieldInfo));
    }

    private T element;

    public Row(DataRow row)
    {
        this.element = new T();

        foreach (FieldDefinition fieldDefinition in fieldDefinitions)
            fieldDefinition.Read(element, row);
    }

    XmlSchema IXmlSerializable.GetSchema() { return null; }

    void IXmlSerializable.ReadXml(XmlReader reader) { throw new NotImplementedException(); }

    void IXmlSerializable.WriteXml(XmlWriter writer)
    {
        foreach (FieldDefinition fieldDefinition in fieldDefinitions)
            fieldDefinition.Write(element, writer);
    }
}
  • 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-20T10:55:24+00:00Added an answer on May 20, 2026 at 10:55 am

    Although you can’t do that, the code that processes the attribute can check the second string, and, if it’s null, get the field name that this attribute is applied to.

    Simple solution for your example:

    public class WSField : Attribute {
      // ...
      internal string xmlField;
      public WSField(string tableField, string xmlField = null) {
    
    ...
    
    public class FieldDefinition {
      public FieldDefinition(FieldInfo fieldInfo) {
        // ...
        if (this.wsField.xmlField == null) this.wsField.xmlField = fieldInfo.Name;
      }
    
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my user model, I have an attribute called nickname and validates as such:
I have a filter attribute that I'd like to be applied to every action
I have the following code: for attribute in site.device_attributes device.attribute end where I would
I have a class which is marked with a custom attribute, like this: public
I know I could have an attribute but that's more work than I want
I have a custom attribute which can be assigned to a class, [FooAttribute] .
According to HTML specs, the select tag in HTML doesn't have a readonly attribute,
I have a problem applying the DebuggerDisplay attribute on a generic class: [DebuggerDisplay(--foo--)] class
I have a frameset with it's cols attribute set to 50%,50% at the moment.
I have a list of dynamically generated div-tags with a class-attribute but no id.

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.