473,396 Members | 2,129 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,396 software developers and data experts.

Reflection - When to use?

I understand that reflection allows me to discover the metadata of a
class at runtime (properties, methods etc). What I don't understand is
where this is useful. For example:

If I am the sole author of an application, I already know the metadata
about my classes - I wrote them. And even if I did not - Let's say I
buy a 3rd party class lib, it will be documented for me with that lib.

If I am part of a dev team, the same is true - If I use a class that I
did not author, I should still be able to reference the documentation
for that class and use it.

Can someone clarify for me?

Sep 16 '06 #1
5 1847
"heddy" <he*******@gmail.coma écrit dans le message de news:
11**********************@d34g2000cwd.googlegroups. com...

|I understand that reflection allows me to discover the metadata of a
| class at runtime (properties, methods etc). What I don't understand is
| where this is useful. For example:
|
| If I am the sole author of an application, I already know the metadata
| about my classes - I wrote them. And even if I did not - Let's say I
| buy a 3rd party class lib, it will be documented for me with that lib.
|
| If I am part of a dev team, the same is true - If I use a class that I
| did not author, I should still be able to reference the documentation
| for that class and use it.
|
| Can someone clarify for me?

For example, I write many different business classes, and I need to store
instance of these classes in a mechanism that hides the database and
translates those objects into SQL commands.

I write a storage mechanism with methods like this :

class Storage
{
void StoreObject(object obj)
{
PropertyInfo[] properties = obj.GetType().GetProperties();
// read properties and their values
// and create SQL Update or Insert statements
// using the names and values found.
}
...
}

and many other situations where the type of an object could be one of many.

However, reflection is not always necessary for these kind of tasks as the
Visitor design pattern can do this kind of thing

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Sep 16 '06 #2
If I am the sole author of an application, I already know the metadata
about my classes - I wrote them. And even if I did not - Let's say I
buy a 3rd party class lib, it will be documented for me with that lib.
If that's all you have, you won't likely need to deal with reflection in
your scenario.

Here's a common scenario. What if you decided that you wanted to open up
your application to others by offering a plug-in architecture. Your
application then loads in these third party plug-ins at runtime and executes
functions that you previously defined. Reflection would be used to evaluate
the type and member definitions of these plug-ins to determine compatibility
and to invoke the appropriate members at the appropriate times.

Another common scenario is manual serialization. Let's say that you wanted
to provide a class library that could take someone else's objects, serialize
them, transport them to another computer, and then deserialize them, even if
the objects were not marked as serializeable. You could use reflection to
deconstruct these objects, capture the values using preevaluated type
information that was understood using reflection, then reconstruct the
objects, possibly using System.Reflection.Emit.

Generally, .NET reflection is a handy feature that allows you to introspect
the assembly, type, and member information of any .NET solution (including
your own), or to construct new classes at runtime using Emit. However, you
asked a good question in that you should not care about reflection unless
you need it. It should never be used for loose typing, for example, when
strongly typed solutions can be accomplished just as easily, because
strongly typed solutions have better performance and are typically far more
stable and secure.

Jon

"heddy" <he*******@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
>I understand that reflection allows me to discover the metadata of a
class at runtime (properties, methods etc). What I don't understand is
where this is useful. For example:

If I am the sole author of an application, I already know the metadata
about my classes - I wrote them. And even if I did not - Let's say I
buy a 3rd party class lib, it will be documented for me with that lib.

If I am part of a dev team, the same is true - If I use a class that I
did not author, I should still be able to reference the documentation
for that class and use it.

Can someone clarify for me?

Sep 17 '06 #3
Jon already pointed out when its useful to use it. Although useful keep
in mind that its one of the most abused features of .NET and has pretty
significant performance penalty when used improperly.

Check out Scott Hanselmans podcast on reflection.
http://www.hanselminutes.com/default.aspx?showID=37

heddy wrote:
I understand that reflection allows me to discover the metadata of a
class at runtime (properties, methods etc). What I don't understand is
where this is useful. For example:

If I am the sole author of an application, I already know the metadata
about my classes - I wrote them. And even if I did not - Let's say I
buy a 3rd party class lib, it will be documented for me with that lib.

If I am part of a dev team, the same is true - If I use a class that I
did not author, I should still be able to reference the documentation
for that class and use it.

Can someone clarify for me?
Sep 17 '06 #4
A great example is when you want to dynamically discover
something about a class. I've written methods that permit
classes to self validate required fields. Then, have all
by data classes inherit this base class with these methods.

I've written classes that can dynamically copy themselves
to "similar" classes that have slightly different signatures.
Great for unit tests and for windows forms to web service
type implementations.

I've used a similar approach to auto load a class at runtime
with results from a datatable:

http://www.eggheadcafe.com/articles/..._generator.asp

As others have stated, you want to be aware of the performance
penalty. Typically, you avoid this by caching the reflected
results or creating static objects of them. Things like PropertyInfo[]
or FieldInfo[]. So, you perform the reflection once during the life
of your app on a given class. From that point forward, your
app will perform at 99.9% of the speed had you not used
reflection at all. And saved, yourself a ton of typing and
reduced the risk of runtime bugs when things in the database
change.

It can be a huge benefit if you use it wisely.

--
Robbe Morris - 2004-2006 Microsoft MVP C#
Earn money publishing .NET articles at
http://www.eggheadcafe.com


"heddy" <he*******@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
>I understand that reflection allows me to discover the metadata of a
class at runtime (properties, methods etc). What I don't understand is
where this is useful. For example:

If I am the sole author of an application, I already know the metadata
about my classes - I wrote them. And even if I did not - Let's say I
buy a 3rd party class lib, it will be documented for me with that lib.

If I am part of a dev team, the same is true - If I use a class that I
did not author, I should still be able to reference the documentation
for that class and use it.

Can someone clarify for me?

Sep 18 '06 #5
Hi,
"heddy" <he*******@gmail.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
>I understand that reflection allows me to discover the metadata of a
class at runtime (properties, methods etc). What I don't understand is
where this is useful. For example:

Can someone clarify for me?
I thikn that the best example is Binding , when you bind a collection of
objects (or even a strong typed dataset) to a grid for example it use
reflection to know about the properties of the objects and create the
columns.

Another good example is a generic sorter class, see my post in the "User
class sorting" thread. basically you have a class that implement IComparer
that accept as parameter the property name over which it will sort a
collection.
In general reflection is used a lot.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Sep 18 '06 #6

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

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
0
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to...
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
2
by: diego | last post by:
hi everyone, i have a sub that opens a form given the form's name as string and opens it using System.Reflection. How can I set the form's properties at runtime. Here is my code. Public Sub...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.