473,569 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PropertyInfo.Se tValue()

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.Se tValue() at runtime. Can anyone tell me 1) how
expensive is the call to PropertyInfo.Se tValue() ? 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 15995
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.Diagnost ics 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.co m

"FourEyes" <N.**********@g mail.comwrote in message
news:11******** *************@o 38g2000hse.goog legroups.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.Se tValue() at runtime. Can anyone tell me 1) how
expensive is the call to PropertyInfo.Se tValue() ? 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.Se tValue() at runtime. Can anyone tell me 1) how
expensive is the call to PropertyInfo.Se tValue() ? 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.guar d.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.Diagnost ics 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.c om

"FourEyes" <N.AhmedRo...@g mail.comwrote in message

news:11******** *************@o 38g2000hse.goog legroups.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.Se tValue() at runtime. Can anyone tell me 1) how
expensive is the call to PropertyInfo.Se tValue() ? 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<pr opertyName,prop ertyValuein
newPropertyValu es )
{
objA.SetPropert y(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...@g mail.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.co mwrote:
On Nov 6, 3:02 pm, FourEyes <N.AhmedRo...@g mail.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.Se tValue(), but I would like to get better
performance without the maintenance cost of a complex solution.

Thanks,
Nora

Nov 7 '07 #7
Norapinephrine <N.**********@g mail.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.Se tValue(), 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 PropertyDescrip tor instead of PropertyInfo gives me about the
same performance.

If this isn't good enough, see whether Marc Gravell's
"HyperDescripto r" code helps you:

http://www.codeproject.com/csharp/Hy...Descriptor.asp

using System;
using System.Diagnost ics;
using System.Reflecti on;
using System.Collecti ons.Generic;

public class LotsOfPropertie s
{
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<PropertyIn foproperties = new List<PropertyIn fo>();
for (int i=0; i < 20; i++)
{
properties.Add( typeof(LotsOfPr operties).
GetProperty("Fo o"+i));
}

Stopwatch sw = Stopwatch.Start New();
for (int i=0; i < 66000; i++)
{
object o = Activator.Creat eInstance
(typeof(LotsOfP roperties));
foreach (PropertyInfo prop in properties)
{
prop.SetValue(o , "Value", null);
}
}
sw.Stop();
Console.WriteLi ne (sw.ElapsedMill iseconds);
}
}

--
Jon Skeet - <sk***@pobox.co m>
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.co mwrote:
Norapinephrine <N.AhmedRo...@g mail.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.Se tValue(), 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 PropertyDescrip tor instead of PropertyInfo gives me about the
same performance.

If this isn't good enough, see whether Marc Gravell's
"HyperDescripto r" code helps you:

http://www.codeproject.com/csharp/Hy...Descriptor.asp

using System;
using System.Diagnost ics;
using System.Reflecti on;
using System.Collecti ons.Generic;

public class LotsOfPropertie s
{
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<PropertyIn foproperties = new List<PropertyIn fo>();
for (int i=0; i < 20; i++)
{
properties.Add( typeof(LotsOfPr operties).
GetProperty("Fo o"+i));
}

Stopwatch sw = Stopwatch.Start New();
for (int i=0; i < 66000; i++)
{
object o = Activator.Creat eInstance
(typeof(LotsOfP roperties));
foreach (PropertyInfo prop in properties)
{
prop.SetValue(o , "Value", null);
}
}
sw.Stop();
Console.WriteLi ne (sw.ElapsedMill iseconds);
}

}

--
Jon Skeet - <sk...@pobox.co m>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.Se tValue() 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 PropertyDescrip tor) - so what I did was to
build a PropertyDescrip tor-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
7204
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 passed-in SqlCommand object. I can find the properties OK but am unsuccessful in trying to change their value due to the way I have identified...
2
402
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
1535
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 I'm trying to pass into SetValue as the correct type? Bob
15
8197
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 also a minor change in class Mypropertyinfo, which I have commented out. When using a structure, however, the second call to GetValue returns...
1
3940
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") oPropertyInfo.SetValue(oControl, "Some New Text", Nothing)
3
6208
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: pi.SetValue(settingsObject, GetSettingFromDb(attribute.SettingName), null); When I run this, I receive the following error:
0
1416
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 how To call my custom class. My code like this : public abstract class Class2 : PropertyInfo //, RuntimePropertyInfo
2
2508
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) host.GetService(typeof(IContainer)); IComponent component1 = container1.Components; PropertyInfo info1 = component1.GetType().GetProperty(this.member.Name);
1
4854
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 writing the following types of structure over and over. Is there something I'm missing? Or some 'funky' 3.5 delegate/lambda expression way of doing this?...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.