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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:51:46+00:00 2026-06-12T21:51:46+00:00

I have a performance problem because I use reflection and GetCustomAttributes for my data

  • 0

I have a performance problem because I use reflection and GetCustomAttributes for my data access. The performance profiler detected it. I have an extension method like this:

public static class DataRowExtensions
{
    /// <summary>
    /// Maps DataRow objecto to entity T depending on the defined attributes. 
    /// </summary>
    /// <typeparam name="T">Entity to map.</typeparam>
    /// <param name="rowInstance">DataRow instance.</param>
    /// <returns>Instance to created entity.</returns>
    public static T MapRow<T>(this DataRow rowInstance) where T : class, new()
    {
        //Create T item
        T instance = new T();

        IEnumerable<PropertyInfo> properties = typeof(T).GetProperties();
        MappingAttribute map;
        DataColumn column;

        foreach (PropertyInfo item in properties)
        {
            //check if custom attribute exist in this property
            object[] definedAttributes = item.GetCustomAttributes(typeof(MappingAttribute), false);

            // Tiene atributos
            if (definedAttributes != null && definedAttributes.Length == 1)
            {
                //recover first attribute
                map = definedAttributes.First() as MappingAttribute;

                column = rowInstance.Table.Columns.OfType<DataColumn>()
                                          .Where(c => c.ColumnName == map.ColumnName)
                                          .SingleOrDefault();

                if (column != null)
                {
                    object dbValue = rowInstance[column.ColumnName];
                    object valueToSet = null;

                    if (dbValue == DBNull.Value)//if value is null
                        valueToSet = map.DefaultValue;
                    else
                        valueToSet = dbValue;

                    //Set value in property 
                    setValue<T>(instance, item, valueToSet);
                }
            }
        }

        return instance;
    }

    /// <summary>
    /// Set "item" property.
    /// </summary>
    /// <typeparam name="T">Return entity type</typeparam>
    /// <param name="instance">T type instance</param>
    /// <param name="item">Property name to return value</param>
    /// <param name="valueToSet">Value to set to the property</param>
    private static void setValue<T>(T instance, PropertyInfo item, object valueToSet) where T : class, new()
    {
        if (valueToSet == null)
        {
            CultureInfo ci = CultureInfo.InvariantCulture;

            if (item.PropertyType.IsSubclassOf(typeof(System.ValueType)))
            {
                //if is a value type and is nullable
                if (item.PropertyType.FullName.Contains("System.Nullable"))
                {
                    item.SetValue(instance, null, BindingFlags.Public, null, null, ci);
                }
                else
                {
                    item.SetValue(instance, Activator.CreateInstance(item.PropertyType, null), BindingFlags.Public, null, null, ci);
                }
            }
            else //property type is reference type
            {
                item.SetValue(instance, null, BindingFlags.Public, null, null, ci);
            }
        }
        else // set not null value
        {
            //if is a value type and is nullable
            if (item.PropertyType.FullName.Contains("System.Nullable"))
            {
                item.SetValue(instance, Convert.ChangeType(valueToSet, Nullable.GetUnderlyingType(item.PropertyType)), null);
            }
            else
            {
                item.SetValue(instance, Convert.ChangeType(valueToSet, item.PropertyType), null);
            }
        }
    }
}

What I do here, in essence, is to map the domain entities with the database fields, and a data helper attacks the tables automatically. An example of one of these entities is:

public class ComboBox
    {
    /// <summary>
    /// Represents a ComboBox item.
    /// </summary>
    [Mapping("CODE", DefaultValue = 0, DBType = DbParametersTypes.Varchar2, IsKey = true, IdentifierFK = "")]
    public string Code { get; set; }

    /// <summary>
    /// Represents Text.
    /// </summary>
    [Mapping("DESCRIPTION", DefaultValue = "", DBType = DbParametersTypes.Varchar2, IsKey = false, IdentifierFK = "")]
    public string Description { get; set; }

    }

And the attribute class I use:

public sealed class MappingAttribute : Attribute
    {
        public string ColumnName { get; set; }

        public object DefaultValue { get; set; }

        public DbParametersTypes DBType { get; set; }

        public bool IsKey { get; set; }

        public string IdentifierFK { get; set; }

        public bool IsParameter { get; set; } 

        public MappingAttribute(string columnName)
        {
            if (String.IsNullOrEmpty(columnName))
                throw new ArgumentNullException("columnName");

            ColumnName = columnName;
        }               
    }

I read here that a possible improvement could be an expression tree, but, first, I’m not an expression tress expert, and second, I have to solve this with .NET 3.5…(in the sample .NET 4 or 4.5 is used…)

¿Suggestions?

Thanks in advance.

  • 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-12T21:51:47+00:00Added an answer on June 12, 2026 at 9:51 pm
    public static class DataRowExtensions
    {
        /// <summary>
        /// Maps DataRow objecto to entity T depending on the defined attributes. 
        /// </summary>
        /// <typeparam name="T">Entity to map.</typeparam>
        /// <param name="rowInstance">DataRow instance.</param>
        /// <returns>Instance to created entity.</returns>
        public static T MapRow<T>( this DataRow rowInstance ) where T : class, new()
        {
            //Create T item
            var instance = new T();
            Mapper<T>.MapRow( instance, rowInstance );
            return instance;
        }
    
        #region Nested type: Mapper
    
        private static class Mapper<T>
            where T : class
        {
            private static readonly ItemMapper[] __mappers;
    
            static Mapper()
            {
                __mappers = typeof (T)
                    .GetProperties()
                    .Where( p => p.IsDefined( typeof (MappingAttribute), false ) )
                    .Select( p => new
                    {
                        Property = p,
                        Attribute = p
                                      .GetCustomAttributes( typeof (MappingAttribute), false )
                                      .Cast<MappingAttribute>()
                                      .FirstOrDefault()
                    } )
                    .Select( m => new ItemMapper( m.Property, m.Attribute ) )
                    .ToArray();
            }
    
            public static void MapRow( T instance, DataRow row )
            {
                foreach ( var mapper in __mappers )
                {
                    mapper.MapRow( instance, row );
                }
            }
    
            #region Nested type: ItemMapper
    
            private sealed class ItemMapper
            {
                private readonly MappingAttribute _attribute;
                private readonly PropertyInfo _property;
    
                public ItemMapper( PropertyInfo property, MappingAttribute attribute )
                {
                    _property = property;
                    _attribute = attribute;
                }
    
                public void MapRow( T instance, DataRow rowInstance )
                {
                    //TODO: Implement this with the code already provided
                }
            }
    
            #endregion
        }
    
        #endregion
    }
    

    The first time the extension method is called for a given <T>, the static constructor will run and cache an instance Mapper for each property that has a MappingAttribute attached. Then, for every call after that, it will used the cached mappers to do the actual copy.

    You can also make Mapper abstract, and use a different subclass for each branch in your setValue<T>(). That way most of your reflection only happens once.

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

Sidebar

Related Questions

I have a serious performance problem. I have a database with (related to this
This is Uday. I have a problem with the write performance with a small
We have a huge performance problem in our app, we collect data from different
I have a performance problem related to string comparison (in Java). I'm working on
I have a performance problem on a query. First table is a Customer table
I have got a performance problem about TextField.htmlText +=msg .And I know that TextField.appendText(msg)
So I have a bit of a performance problem. I have made a java
I have problem with my widget related to performance and memory: Issue : My
I have a problem with SHA-1 performance on Android. In C# I get calculated
The problem: I have a WCF service that I'm trying to performance test with

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.