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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:39:50+00:00 2026-05-29T08:39:50+00:00

its me again! :) I’ve been tasked with creating a system to auditing out

  • 0

its me again! 🙂

I’ve been tasked with creating a system to auditing out internal objects, my first iteration was no flexible and very slow so I’m hoping to rewrite it and really make it work how it should.

Performance for this needs to be as perfect as possible, the auditing code is probably going to be run on every object in our system when they are saved..

This code below is what I’ve done so far – I’ve profiled it using the visual studio tools and I think I’ve remove quite a few possible performance hits along the way..

What I really want from you guys is to review this and suggest any possible improvements, Its also worth nothing that CreateObjectFromHistory method doesn’t need to be as performant as the rest, its barely going to ever get called.

Also – they keyvaluepair saving is out of my hands.

Any help would be brilliant..

Cheers 🙂

//Wall o-code coming up..

    public static void AuditObject(ITraceable obj)
    {
        if (obj == null)
            return;

        IEnumerable<PropertyInfo> properties = GetPropertyInfo(obj);

        List<SerializeableKeyValuePair<string, object>> kvpList =
            new List<SerializeableKeyValuePair<string, object>>();

        foreach (PropertyInfo property in properties)
        {
            SerializeableKeyValuePair<string, object> thisValue = new SerializeableKeyValuePair<string, object>();

            thisValue.Key = property.Name;

            thisValue.Value = GetPropertyValue(obj, property);

            if (thisValue.Value != null)
                kvpList.Add(thisValue);
        }

        TestObject o = CreateObjectFromHistory<TestObject>(kvpList);
    }

    public static T CreateObjectFromHistory<T>(List<SerializeableKeyValuePair<string, object>> history)
        where T : class, ITraceable
    {
        T historicalObject = Activator.CreateInstance<T>();

        Dictionary<string, PropertyInfo> propertys = GetPropertysAsDictionary(historicalObject);

        foreach (SerializeableKeyValuePair<string, object> kvp in history)
        {
            if (!propertys.ContainsKey(kvp.Key))
                continue;

            PropertyInfo prop = propertys[kvp.Key];

            if (prop == null)
                continue;

            var value = CoerceValue(prop.PropertyType, kvp.Value);

            prop.SetValue(historicalObject, value, null);

        }

        return historicalObject;

    }

    private static object CoerceValue(Type type, object value)
    {
        if (type == typeof(string))
            return value as string;

        return null;
    }

    private static object GetPropertyValue(ITraceable obj, PropertyInfo property)
    {
        if (property.PropertyType == typeof(string))
            return GetProperyValueByType<string>(property.GetValue(obj, null));
        else if (property.PropertyType == typeof(DateTime))
            return GetProperyValueByType<DateTime>(property.GetValue(obj, null));

        return null;
    }

    private static IEnumerable<PropertyInfo> GetPropertyInfo(ITraceable obj)
    {
        List<PropertyInfo> properties;

        Type objType = obj.GetType();

        if (PropertyDictionary.TryGetValue(objType, out properties) == false)
        {
            properties = obj.GetType().GetProperties(BindingFlags.Public |
                                                     BindingFlags.Instance).ToList();

            properties.RemoveAll(p => IgnoreProperty(p.GetCustomAttributes(typeof(DoNoTraceAttribute), false)));

            PropertyDictionary.Add(objType, properties);
        }

        return properties;
    }

    private static Dictionary<string, PropertyInfo> GetPropertysAsDictionary(ITraceable obj)
    {
        return GetPropertyInfo(obj).ToDictionary(pro => pro.Name);
    }

    private static object GetProperyValueByType<T>(object value)
    {
        T actualType = (T)value;

        if (actualType.Equals(default(T)))
            return default(T);

        //this will need further implementation

        return (T)value;
    }

    private static bool IgnoreProperty(IEnumerable<object> p)
    {
        return p.AsParallel().OfType<DoNoTraceAttribute>().Any();
    }

Updated Code;

   private static IEnumerable<PropertyInfo> GetPropertyInfo(ITraceable obj)
    {
        List<PropertyInfo> properties;

        Type objType = obj.GetType();

        if (PropertyDictionary.TryGetValue(objType, out properties) == false)
        {
            properties = obj.GetType().GetProperties(BindingFlags.Public |
                                                     BindingFlags.Instance).ToList();

            properties.RemoveAll(p => Attribute.IsDefined(p, typeof(DoNoTraceAttribute)));

            PropertyDictionary.Add(objType, properties);
        }

        return properties;
    }

Do this look better ?

  • 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-29T08:39:51+00:00Added an answer on May 29, 2026 at 8:39 am

    If you use PropertyInfo.GetValue() at runtime, the performance will always be slow. To get good performance (especially for looking at lots of objects) you will need to look at something like ILGenerator or Expression – or you could just use something like FastMember and access the values via prop.Name. I really do not think IgnoreProperty is implemented well – you should just look at Attribute.IsDefined here; no need for LINQ, no need for Parallel, and no need to materialize the attributes.

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

Sidebar

Related Questions

I'm trying to get into java again (it's been a few years). I never
its out of question that a dao will not hold any state. however, for
Its been a while I have ready Mcconnell's Code Complete . Now I read
its me again with a php problem :) Following is part of my PHP
Its me again come up with another question, how to adjust padding between each
My application is calling wcf service. First time its call & list all table
When WPF window appear first time, its content seem frozen. To refresh content I
its me again. I tried the last hours, how to change content of a
its templates again ;-) Given the following template member functions and CRTP class: template<typename
Hey guys its me again with another easy one. I want to center the

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.