Norapinephrine <N.**********@gmail.comwrote:
We have an old version of the product which is taking a lot of time to
complete, for various reasons. I can see now that for the case of
creating 66K objects with about 20 properties that need to be set, it
is quite slow. I will of course time the process with and without
using PropertyInfo.SetValue(), but I would like to get better
performance without the maintenance cost of a complex solution.
How slow is it? On my laptop, I can do that in just over 7 seconds
(code below - I've used C# 3 for automatic properties, but that's all).
How often are you doing this?
Using PropertyDescriptor instead of PropertyInfo gives me about the
same performance.
If this isn't good enough, see whether Marc Gravell's
"HyperDescriptor" code helps you:
http://www.codeproject.com/csharp/Hy...Descriptor.asp
using System;
using System.Diagnostics;
using System.Reflection;
using System.Collections.Generic;
public class LotsOfProperties
{
public string Foo0 { get; set; }
public string Foo1 { get; set; }
public string Foo2 { get; set; }
public string Foo3 { get; set; }
public string Foo4 { get; set; }
public string Foo5 { get; set; }
public string Foo6 { get; set; }
public string Foo7 { get; set; }
public string Foo8 { get; set; }
public string Foo9 { get; set; }
public string Foo10 { get; set; }
public string Foo11 { get; set; }
public string Foo12 { get; set; }
public string Foo13 { get; set; }
public string Foo14 { get; set; }
public string Foo15 { get; set; }
public string Foo16 { get; set; }
public string Foo17 { get; set; }
public string Foo18 { get; set; }
public string Foo19 { get; set; }
}
public class Test
{
static void Main()
{
List<PropertyInfoproperties = new List<PropertyInfo>();
for (int i=0; i < 20; i++)
{
properties.Add(typeof(LotsOfProperties).
GetProperty("Foo"+i));
}
Stopwatch sw = Stopwatch.StartNew();
for (int i=0; i < 66000; i++)
{
object o = Activator.CreateInstance
(typeof(LotsOfProperties));
foreach (PropertyInfo prop in properties)
{
prop.SetValue(o, "Value", null);
}
}
sw.Stop();
Console.WriteLine (sw.ElapsedMilliseconds);
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too