Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Reflection?

Please tell me what reflection means.
[37 byte] By [Faraw] at [2007-11-9 18:21:21]
# 1 Re: Reflection?
"Faraw" <betemariam@hotmail.com> wrote in message
news:39919c76$1@news.dev-archive.com...
>
> Please tell me what reflection means.

AIUI: You can dynamically at run time get various information about the
classes, not only your own, and do everything from checking attributes to
get a list of all the methods (simply get all the information the run-time
has about the classes, granted you're code is trusted to do that).

/Carl
Carl Nettelblad at 2007-11-11 22:29:29 >
# 2 Re: Reflection?
Thanks a lot Carl.

"Carl Nettelblad" <cnettel@hem.passagen.se> wrote:
>
>"Faraw" <betemariam@hotmail.com> wrote in message
>news:39919c76$1@news.dev-archive.com...
>>
>> Please tell me what reflection means.
>
>AIUI: You can dynamically at run time get various information about the
>classes, not only your own, and do everything from checking attributes to
>get a list of all the methods (simply get all the information the run-time
>has about the classes, granted you're code is trusted to do that).
>
>/Carl
>
>
Faraw at 2007-11-11 22:30:23 >
# 3 Re: Reflection?
"Carl Nettelblad" <cnettel@hem.passagen.se> wrote in message
news:3991ab0d@news.dev-archive.com...
>
> "Faraw" <betemariam@hotmail.com> wrote in message
> news:39919c76$1@news.dev-archive.com...
> >
> > Please tell me what reflection means.
>
> AIUI: You can dynamically at run time get various information about the
> classes, not only your own, and do everything from checking attributes to
> get a list of all the methods (simply get all the information the run-time
> has about the classes, granted you're code is trusted to do that).

You can also do things like creating instances of objects and calling
functions on them, in a late-bound fashion.
Eric Gunnerson at 2007-11-11 22:31:33 >
# 4 Re: Reflection?
What is the relationship between Reflection and Attributes? I still need to
study the little blurb on attributes more closely, but on first read, I get
the impression that Reflection is a property of languages in general, and C#
attributes are how you get at Reflection in the case of C#. Am I close?

"Eric Gunnerson" <ericgu@nospam.microsoft.nospam.com> wrote in message
news:3991aff9@news.dev-archive.com...
> "Carl Nettelblad" <cnettel@hem.passagen.se> wrote in message
> news:3991ab0d@news.dev-archive.com...
> >
> > "Faraw" <betemariam@hotmail.com> wrote in message
> > news:39919c76$1@news.dev-archive.com...
> > >
> > > Please tell me what reflection means.
> >
> > AIUI: You can dynamically at run time get various information about the
> > classes, not only your own, and do everything from checking attributes
to
> > get a list of all the methods (simply get all the information the
run-time
> > has about the classes, granted you're code is trusted to do that).
>
> You can also do things like creating instances of objects and calling
> functions on them, in a late-bound fashion.
>
>
Oscar at 2007-11-11 22:32:33 >
# 5 Re: Reflection?
I suspect C# attributes are seperate from reflection.

As far as I know, C# attributes allow you to quickly persist an object and
all it's attributes into XML format. Something I've been looking forward
to, since I keep having to manually do it in java :/

--
-
Thong Nguyen
http://tummy.veridicus.com

"Oscar" <consulBanana@hotmail.com> wrote in message
news:39921ebc$1@news.dev-archive.com...
> What is the relationship between Reflection and Attributes? I still need
to
> study the little blurb on attributes more closely, but on first read, I
get
> the impression that Reflection is a property of languages in general, and
C#
> attributes are how you get at Reflection in the case of C#. Am I close?
>
> "Eric Gunnerson" <ericgu@nospam.microsoft.nospam.com> wrote in message
> news:3991aff9@news.dev-archive.com...
> > "Carl Nettelblad" <cnettel@hem.passagen.se> wrote in message
> > news:3991ab0d@news.dev-archive.com...
> > >
> > > "Faraw" <betemariam@hotmail.com> wrote in message
> > > news:39919c76$1@news.dev-archive.com...
> > > >
> > > > Please tell me what reflection means.
> > >
> > > AIUI: You can dynamically at run time get various information about
the
> > > classes, not only your own, and do everything from checking attributes
> to
> > > get a list of all the methods (simply get all the information the
> run-time
> > > has about the classes, granted you're code is trusted to do that).
> >
> > You can also do things like creating instances of objects and calling
> > functions on them, in a late-bound fashion.
> >
> >
>
>
Thong Nguyen at 2007-11-11 22:33:25 >
# 6 Re: Reflection?
Reflection is a service of the runtime. You can walk up to an assembly or
type, find out about the types and methods it contains, create instances of
them, call functions on them, etc.

Attributes are a way of putting information with the metadata of an
assembly, type (class/struct/enum), member, parameter, or some other entity.
This information can then be queried using reflection; once you have the
type object for something, you can find out the attributes that are applied
to it.

For example, if I have an attribute class like this:

public class HelpUrlAttribute : System.Attribute
{
public HelpUrlAttribute(string url) { . }
public string Url { get {.} }
}

I can then use it like this:

[HelpUrl("http://SomeUrl/MyClass")]
class MyClass {}

and query it at runtime with the reflection:

Type type = typeof(MyClass);
foreach(object attr in type.GetCustomAttributes() )
{
if ( attr is HelpUrlAttribute )
{
HelpUrlAttribute ha = (HelpUrlAttribute) attr;
myBrowser.Navigate( ha.Url );
}
}

Does that help?

"Oscar" <consulBanana@hotmail.com> wrote in message
news:39921ebc$1@news.dev-archive.com...
> What is the relationship between Reflection and Attributes? I still need
to
> study the little blurb on attributes more closely, but on first read, I
get
> the impression that Reflection is a property of languages in general, and
C#
> attributes are how you get at Reflection in the case of C#. Am I close?
>
> "Eric Gunnerson" <ericgu@nospam.microsoft.nospam.com> wrote in message
> news:3991aff9@news.dev-archive.com...
> > "Carl Nettelblad" <cnettel@hem.passagen.se> wrote in message
> > news:3991ab0d@news.dev-archive.com...
> > >
> > > "Faraw" <betemariam@hotmail.com> wrote in message
> > > news:39919c76$1@news.dev-archive.com...
> > > >
> > > > Please tell me what reflection means.
> > >
> > > AIUI: You can dynamically at run time get various information about
the
> > > classes, not only your own, and do everything from checking attributes
> to
> > > get a list of all the methods (simply get all the information the
> run-time
> > > has about the classes, granted you're code is trusted to do that).
> >
> > You can also do things like creating instances of objects and calling
> > functions on them, in a late-bound fashion.
> >
> >
>
>
Eric Gunnerson at 2007-11-11 22:34:30 >
# 7 Re: Reflection?
Thanks to both of you. I think that makes things a lot clearer

"Eric Gunnerson" <ericgu@nospam.microsoft.nospam.com> wrote in message
news:3992e100$1@news.dev-archive.com...
> Reflection is a service of the runtime. You can walk up to an assembly or
> type, find out about the types and methods it contains, create instances
of
> them, call functions on them, etc.
>
> Attributes are a way of putting information with the metadata of an
> assembly, type (class/struct/enum), member, parameter, or some other
entity.
> This information can then be queried using reflection; once you have the
> type object for something, you can find out the attributes that are
applied
> to it.
>
> For example, if I have an attribute class like this:
>
> public class HelpUrlAttribute : System.Attribute
> {
> public HelpUrlAttribute(string url) { . }
> public string Url { get {.} }
> }
>
> I can then use it like this:
>
> [HelpUrl("http://SomeUrl/MyClass")]
> class MyClass {}
>
> and query it at runtime with the reflection:
>
> Type type = typeof(MyClass);
> foreach(object attr in type.GetCustomAttributes() )
> {
> if ( attr is HelpUrlAttribute )
> {
> HelpUrlAttribute ha = (HelpUrlAttribute) attr;
> myBrowser.Navigate( ha.Url );
> }
> }
>
> Does that help?
>
> "Oscar" <consulBanana@hotmail.com> wrote in message
> news:39921ebc$1@news.dev-archive.com...
> > What is the relationship between Reflection and Attributes? I still
need
> to
> > study the little blurb on attributes more closely, but on first read, I
> get
> > the impression that Reflection is a property of languages in general,
and
> C#
> > attributes are how you get at Reflection in the case of C#. Am I close?
> >
> > "Eric Gunnerson" <ericgu@nospam.microsoft.nospam.com> wrote in message
> > news:3991aff9@news.dev-archive.com...
> > > "Carl Nettelblad" <cnettel@hem.passagen.se> wrote in message
> > > news:3991ab0d@news.dev-archive.com...
> > > >
> > > > "Faraw" <betemariam@hotmail.com> wrote in message
> > > > news:39919c76$1@news.dev-archive.com...
> > > > >
> > > > > Please tell me what reflection means.
> > > >
> > > > AIUI: You can dynamically at run time get various information about
> the
> > > > classes, not only your own, and do everything from checking
attributes
> > to
> > > > get a list of all the methods (simply get all the information the
> > run-time
> > > > has about the classes, granted you're code is trusted to do that).
> > >
> > > You can also do things like creating instances of objects and calling
> > > functions on them, in a late-bound fashion.
> > >
> > >
> >
> >
>
>
Oscar at 2007-11-11 22:35:27 >