473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

PropertyInfo.SetValue()

I've read that Reflection is slow and shouldn't be used when
performance is a concern. In my case, performance is definitely a
concern, so I was thinking that I could collect the PropertyInfo
objects that I need at initialization, and then just call
PropertyInfo.SetValue() at runtime. Can anyone tell me 1) how
expensive is the call to PropertyInfo.SetValue() ? and 2) what is a
better alternative ?

I already have a hand-made solution where delegates are stored in a
Dictionary but I am hoping that .NET has something that requires less
code, ie, is auto-generated somehow.

Thanks!

Nov 6 '07 #1
9 15964
Well, using reflection is going to be pretty slow still. You are going
to save some overhead on looking up the PropertyInfo instances, but most of
the time is going to be wasted in calling SetValue.

You can always measure the impact yourself using the StopWatch class in
the System.Diagnostics namespace.

If the properties that you want to set the value of are public (or
available to the code that will be setting the properties), then you can
look into creating a dynamic assembly which will call the property directly,
and assign it the value you want. This would be the fastest way to assign
the value, but it will only work if you have access to the property from the
calling code (if you are trying to call a private property, for example,
then this wouldn't work).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"FourEyes" <N.**********@gmail.comwrote in message
news:11*********************@o38g2000hse.googlegro ups.com...
I've read that Reflection is slow and shouldn't be used when
performance is a concern. In my case, performance is definitely a
concern, so I was thinking that I could collect the PropertyInfo
objects that I need at initialization, and then just call
PropertyInfo.SetValue() at runtime. Can anyone tell me 1) how
expensive is the call to PropertyInfo.SetValue() ? and 2) what is a
better alternative ?

I already have a hand-made solution where delegates are stored in a
Dictionary but I am hoping that .NET has something that requires less
code, ie, is auto-generated somehow.

Thanks!

Nov 6 '07 #2
2) what is a better alternative ?

Not sure if it's a better alternative for you, but there's a method that
worked out well for me. I needed to add plugin capability to an application I
built and the plugins had to be loaded via reflection at the start of the
program for later use. What I did was built an abstract base class containing
the members my app would call. The plugins then had to inherit from this
class.

When I instantiated the classes at runtime, I merely worked with them as the
base type. This let me use reflection only to instantiate the classes. All
the actual calls were handled directly via the base classes interface.

You may be able to use an interface instead of burning the base class in
your case. I needed some common functionality, though, so needed the base
class.
I am hoping that .NET has something that requires less code, ie, is auto generated somehow.
There are code generators available that may suit your needs. Most of the
generators I've seen are specific to database code, however, I think there
are more generic ones available.

You could also build your own. It wouldn't be too much trouble to build a
program that uses reflection to query the class structures and generate code
to do what you need explicitly.

--
Andrew Faust
http://www.andrewfaust.com
"FourEyes" wrote:
I've read that Reflection is slow and shouldn't be used when
performance is a concern. In my case, performance is definitely a
concern, so I was thinking that I could collect the PropertyInfo
objects that I need at initialization, and then just call
PropertyInfo.SetValue() at runtime. Can anyone tell me 1) how
expensive is the call to PropertyInfo.SetValue() ? and 2) what is a
better alternative ?

I already have a hand-made solution where delegates are stored in a
Dictionary but I am hoping that .NET has something that requires less
code, ie, is auto-generated somehow.

Thanks!

Nov 6 '07 #3
On 6 nov, 10:41, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Well, using reflection is going to be pretty slow still. You are going
to save some overhead on looking up the PropertyInfo instances, but most of
the time is going to be wasted in calling SetValue.

You can always measure the impact yourself using the StopWatch class in
the System.Diagnostics namespace.

If the properties that you want to set the value of are public (or
available to the code that will be setting the properties), then you can
look into creating a dynamic assembly which will call the property directly,
and assign it the value you want. This would be the fastest way to assign
the value, but it will only work if you have access to the property from the
calling code (if you are trying to call a private property, for example,
then this wouldn't work).

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"FourEyes" <N.AhmedRo...@gmail.comwrote in message

news:11*********************@o38g2000hse.googlegro ups.com...
I've read that Reflection is slow and shouldn't be used when
performance is a concern. In my case, performance is definitely a
concern, so I was thinking that I could collect the PropertyInfo
objects that I need at initialization, and then just call
PropertyInfo.SetValue() at runtime. Can anyone tell me 1) how
expensive is the call to PropertyInfo.SetValue() ? and 2) what is a
better alternative ?
I already have a hand-made solution where delegates are stored in a
Dictionary but I am hoping that .NET has something that requires less
code, ie, is auto-generated somehow.
Thanks!- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -
Yeah, I haven't got around to timing the SetValue calls yet. I am
rather hesitant to create a dynamic assembly for this solution because
it sounds complicated. I need to be able to easily add classes to the
solution, so I don't want to have anything complicated involved in the
build process or in the code.

However, the solution that I am trying out right now is also more
complicated than I like, because it requires the developers to create
delegates to set/get each field and then to store these into a
dictionary.

Do you think that using dynamic assemblies would be simpler and re-
usable than my method of handwriting delegates for each property and
manually stuffing them into a Dictionary ?

Thanks again!
N

Nov 6 '07 #4
Hmmm... I would like to do something like
>>>
ClassA objA = new ClassA();

foreach( KeyValuePair<propertyName,propertyValuein
newPropertyValues )
{
objA.SetProperty(propertyName, propertyValue);
}
>>>
How would I use a dynamic assembly to do something like that ?

Thanks again,
N
Nov 7 '07 #5
On Nov 6, 3:02 pm, FourEyes <N.AhmedRo...@gmail.comwrote:
I've read that Reflection is slow and shouldn't be used when
performance is a concern. In my case, performance is definitely a
concern
To what extent? Have you got some concrete goals in mind? Using
reflection is slower than not using reflection, but that doesn't mean
it's necessarily *too* slow.

Unless you've got a test bed which measures performance and some
targets, it will be hard to know when you've got a solution which is
deemed acceptable.

Jon

Nov 7 '07 #6
On Nov 7, 9:13 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Nov 6, 3:02 pm, FourEyes <N.AhmedRo...@gmail.comwrote:
I've read that Reflection is slow and shouldn't be used when
performance is a concern. In my case, performance is definitely a
concern

To what extent? Have you got some concrete goals in mind? Using
reflection is slower than not using reflection, but that doesn't mean
it's necessarily *too* slow.

Unless you've got a test bed which measures performance and some
targets, it will be hard to know when you've got a solution which is
deemed acceptable.

Jon
Jon,

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.

Thanks,
Nora

Nov 7 '07 #7
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
Nov 8 '07 #8
On Nov 8, 2:40 am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Norapinephrine <N.AhmedRo...@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
Jon, I hadn't got around to running the stopwatch test yet and your
results make it look like I don't have to worry about the time to run
a PropertyInfo.SetValue() on a given object/value pair. So I'm
guessing that the code is taking too long probably because it is
looking up the property info each time and because the algorithms in
the set properties long.

This is great to know. I'll keep a tab on that link you provided too,
just in case I need it for something else.

Thanks a lot, Jon!
Nora
Nov 9 '07 #9
probably because it is looking up the property info each time

Absolutely; when I was looking into this area (for a flexibile data-
export process), I found that pre-obtaining the getter was a
significant saving by itself (regardless of whether that is a
PropertyInfo or a sneaky PropertyDescriptor) - so what I did was to
build a PropertyDescriptor-array and pre-fill it with the getters that
I needed - then my main output code was essentially:

foreach(row in data) {
startRow();
foreach(prop in propsWanted) {
writeValue(); // from prop and row
}
endRow();
}

Depending on your data, this might be enough... but then, since it
only takes one line of code to swap a reflection-based descriptor for
a dynamic IL-based descriptor, it is trivial to try it before and
after.

Marc

Nov 9 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tony Tullemans | last post by:
I am trying to write a subroutine that will examine all the properties of a form to determine which of those are SqlCommand objects, and then set the Connection property of those SqlCommands to a...
2
by: Hugo Leonardo | last post by:
I'll big problem. I have tree class: public Class Column { public Column {...} string _Value; public bool HasValue = false;
0
by: Bob | last post by:
This works only when the property return type is string: SomeObjectsPropertyInfo.SetValue(SomeObject, SomeStringValue, Nothing) Is there a good way to try to automatically cast the string value...
15
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is...
1
by: Paul | last post by:
This part is easy: Dim oControl As Control oControl = Me.Page.FindControl("TextBox1") Dim oPropertyInfo As PropertyInfo oPropertyInfo = oControl.GetType.GetProperty("Text") ...
3
by: ThisBytes5 | last post by:
I am trying to save/retreive some settings from a database using reflection an my custom attributes. The field in teh database is a string, but my property is a boolen. I have the following code: ...
0
by: tatilou | last post by:
Hello, I need add new method to my properties. these methods should give me some attributes coming from my database. I think, to create a class with implement PropertyInfo but I don't know...
2
by: Carlos Rodriguez | last post by:
I have the following function in C#public void Undo(IDesignerHost host) { if (!this.componentName.Equals(string.Empty) && (this.member != null)) { IContainer container1 = (IContainer)...
1
by: damiensawyer | last post by:
Hi, I need to invoke .getvalue and .setvalue on fieldinfo and propetyinfo objects. Even though they're both derived from MemberInfo, they don't share those two methods. I've finding myself...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.