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

  • Home
  • SEARCH
  • 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 7488173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:46:27+00:00 2026-05-29T14:46:27+00:00

Currently, i have a declaration in a root class that iterates through the properties

  • 0

Currently, i have a declaration in a root class that iterates through the properties of a deriving class and instantiates the Default Value of the property, using the DefaultValueAttribute descriptor. What i want to do is expand this from simply being for DefaultValue to also be XmlElement, XmlAttribute and the series of Attributes included in the Serialization of the Xml namespace.

I am having a problem with expanding the current design to handle multiple attributes without loading a ton of if/then/else statements to handle the various Defined Attributes.

Current Design:

private void Initialize () {
  foreach( PropertyDescriptor property in TypeDescriptor.GetProperties( this ) ) {
    XmlElementAttribute xel = ( XmlElementAttribute ) property.Attributes[typeof( XmlElementAttribute )];
    if( xel != null ) {
      this.AddElement( xel.ElementName , "" );
    }
    DefaultValueAttribute attr = ( DefaultValueAttribute ) property.Attributes[typeof( DefaultValueAttribute )];
    if( attr != null ) {
      property.SetValue( this , attr.Value );
    }
  }
}

Suggestions Design:

private void Initialize () {
  foreach( PropertyDescriptor property in TypeDescriptor.GetProperties( this ) ) {
    foreach( Attribute attr in property.Attributes ) {
      if( attr = typeof(XmlElementAttribute)){
        //do something
      }else if(attr = typeof(DefaultValueAttribute)){
        //do something
      }
    }
  }
}
  • 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-29T14:46:28+00:00Added an answer on May 29, 2026 at 2:46 pm

    You could define a Dictionary<Type, Action<object>> (or replace object with the specific type of your class) and add the code you want to execute for each type:

    var dict = new Dictionary<Type, Action<object>>();
    dict.Add(typeof(XmlElementAttribute), obj =>
    {
      //do something
    });
    

    Now you can just test whether your dictionary contains the type and execute the delegate:

    foreach(Attribute attr in property.Attributes) 
    {  
       var attributeType = attr.GetType();
       if(dict.ContainsKey(attributeType))
       {
         dict[attributeType](this);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using latexsuite with vim and I am currently editing files that have the
I currently have an MS Access application that connects to a PostgreSQL database via
I currently have an existing database and I am using the LINQtoSQL generator tool
I currently have a class and I'm trying to create an easy GUI to
I currently have a DetailsView in ASP.NET that gets data from the database based
I'm using Delphi 7 and Firebird 1.5. I have a query that I create
I have a DataTable that I am creating and binding to a GridView. Currently
I have several classes in an application that I am currently building, and I
I have a Rails app (v2.3.8) using HAML (v3.0.25). My application.html.haml currently has the
I have a function that currently takes in two template parameters. One is expected

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.