"Reflection provides objects (of type
Type) that describe assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.
If you are using attributes in your code, reflection enables you to access them."
Reflection is an advanced technique that one is not too likely to use in the early stages of learning to program in
c#.
damon2012, you should learn to use TDD in order to improve the way you write your programs ... to get an example of TDD (one that uses NUnit, the predecessor of xUnit.net), watch
these two videos (http://www.asp.net/mvc):
1. Introduction 35 minutes
9. Unit Testing 37 mins
pretend that you have written this simple method as part of your ASP.NET program:
public int DivideNumbers(int theTop, int theBottom)
{
return theTop / theBottom;
}
to unit test the above method to ensure that it works, you can write tests like this:
[Fact]
public void FourDividedByTwoIsTwo()
{
Double result = DivideNumbers(4, 2);
Assert.Equal(4.0, result);
}
The above unit test should work ... however, what happens if some day in the future, another programmer attempts to improve your
DivideNumbers method and breaks it? (Answer: your unit test will fail and you will know that your
DivideNumbers method needs to be repaired.) Note: your end users never use your
DivideNumbers method ... it is used by the xUnit.net
unit testing framework which will use reflection at run time to locate
unit tests, i.e., methods that are marked with the
[Fact]attribute.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
How does reflection relate to the use of reflector. I though reflector allows you to look open dlls and view the contents. So how are these things related and what does the word relector actually mean?
Standard ... Basic decompilation
Just .NET Reflector Desktop
Decompile, browse and analyze .NET code
VS ... Decompile in Visual Studio
Dynamic decompilation in Visual Studio
Includes .NET Reflector Desktop and Visual Studio extension
VSPro ... Debug 3rd party code
Generate PDBs for decompiled code
Step into any code and set breakpoints
Dynamic decompilation in Visual Studio
Includes .NET Reflector Desktop and Visual Studio extension
Note:
(a) i do not use this tool
(b) it's not free
(c) most likely, it uses reflection for at least some of what it does.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
No, the word "reflector" does not mean that at all.
A reflector is typically something the reflects (typically) energy, such as mirrors and the materials that are used in high visibility clothing.
Programmers, however, often talk about "reflecting over code" to mean using code to examine built code. There are lots of use cases for this, such as unit testing, locating imports (with MEF), validation, the routing engine in ASP.NET and ASP.NET MVC, assembly
loading, serialization, WCF, and so on and so on.
We often use attributes in our code to facilitate this - such as [DataContract] and [OperationContract] in WCF, the [Imports] attribute in MEF, and the filter attributes on action methods in ASP.NET MVC.
The (no longer free) product, Reflector, is a tool that not only uses the Reflection APIs to examine code, but goes further by decompiling the actual IL blocks of the methods and reverse engineers these back into C#, VB.NET and other .NET languages. This
is, of course, exceptionally useful as it lets you examine actual code. This is really handy when you want to understand, say, how lambda expressions and variable capture actually works, or what is really generated when you write iterator methods with yield
return.
Regards
Dave
Marked as answer by Amy Peng - MSFT on Dec 21, 2012 12:29 AM
damon2012
Member
21 Points
106 Posts
Can someone explain this reflection please
Dec 13, 2012 08:52 PM|LINK
Hi,
I don't fully understand this and why it is known as reflection? Can someone please briefly explain. I tried looking it up but it hasnt clicked yet.
methodbase.getcurrentmethod().name
Thanks.
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: Can someone explain this reflection please
Dec 14, 2012 04:59 AM|LINK
name of current method.
How do you have, for every method, method name?
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Can someone explain this reflection please
Dec 14, 2012 08:34 AM|LINK
@ damon2012
http://msdn.microsoft.com/en-us/library/ms173183.aspx "Reflection (C# and Visual Basic)"
"Reflection provides objects (of type Type) that describe assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them."
Reflection is an advanced technique that one is not too likely to use in the early stages of learning to program in c#.
MORE INFORMATION
see http://xunit.codeplex.com/wikipage?title=Comparisons&referringTitle=Home
"How does xUnit.net compare to other .NET testing frameworks?
damon2012, you should learn to use TDD in order to improve the way you write your programs ... to get an example of TDD (one that uses NUnit, the predecessor of xUnit.net), watch these two videos (http://www.asp.net/mvc):
1. Introduction 35 minutes 9. Unit Testing 37 mins
xUnit.net and other .NET unit testing frameworks require that the programmer use attributes to mark their methods with information that can be found via reflection. Example: http://xunit.codeplex.com/wikipage?title=HowToUse&referringTitle=Home
"How do I use xUnit.net?"
pretend that you have written this simple method as part of your ASP.NET program:
public int DivideNumbers(int theTop, int theBottom) { return theTop / theBottom; }to unit test the above method to ensure that it works, you can write tests like this:
[Fact] public void FourDividedByTwoIsTwo() { Double result = DivideNumbers(4, 2); Assert.Equal(4.0, result); }The above unit test should work ... however, what happens if some day in the future, another programmer attempts to improve your DivideNumbers method and breaks it? (Answer: your unit test will fail and you will know that your DivideNumbers method needs to be repaired.) Note: your end users never use your DivideNumbers method ... it is used by the xUnit.net unit testing framework which will use reflection at run time to locate unit tests, i.e., methods that are marked with the [Fact] attribute.
see http://lmgtfy.com/?q=c%23+reflection for more links about reflection and c#.
Read this article: http://oreilly.com/catalog/progcsharp/chapter/ch18.html
"Chapter 18 Attributes and Reflection" from "Programming C#", Jesse Liberty, July 2001
g.
geniusvishal
Star
13986 Points
2784 Posts
Re: Can someone explain this reflection please
Dec 14, 2012 12:18 PM|LINK
Refer this:
http://forums.asp.net/p/1846891/5160938.aspx/1?p=True&t=634910698942804792
My Website
www.dotnetvishal.com
damon2012
Member
21 Points
106 Posts
Re: Can someone explain this reflection please
Dec 14, 2012 01:13 PM|LINK
Thanks.
How does reflection relate to the use of reflector. I though reflector allows you to look open dlls and view the contents. So how are these things related and what does the word relector actually mean?
Thanks.
RameshRajend...
Star
7983 Points
2099 Posts
Re: Can someone explain this reflection please
Dec 14, 2012 01:16 PM|LINK
http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getcurrentmethod(v=vs.80).aspx
http://stackoverflow.com/questions/4885582/methodbase-getcurrentmethod-performance
http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getcurrentmethod.aspx
damon2012
Member
21 Points
106 Posts
Re: Can someone explain this reflection please
Dec 14, 2012 01:22 PM|LINK
Thanks for that.
How does this acutally relate to reflector the tool for viewing contents of dlls? What does the word reflector actually mean?
Thanks.
geniusvishal
Star
13986 Points
2784 Posts
Re: Can someone explain this reflection please
Dec 14, 2012 04:48 PM|LINK
Routing in General
My Website
www.dotnetvishal.com
gerrylowry
All-Star
20513 Points
5712 Posts
Re: Can someone explain this reflection please
Dec 14, 2012 07:40 PM|LINK
@ damon2012
please note that it's in your best interest if you learn to use Google and/or your favourite search engine(s).
damon2012 ... this appears to me to be a new question ... for new questions, it is almost always best to start a new thread.
http://lmgtfy.com/?q=reflector+.net
Do you mean: http://www.reflector.net/? Please clarify.
http://www.reflector.net/compare/:
Standard ... Basic decompilation
Just .NET Reflector Desktop
Decompile, browse and analyze .NET code
VS ... Decompile in Visual Studio
Dynamic decompilation in Visual Studio
Includes .NET Reflector Desktop and Visual Studio extension
VSPro ... Debug 3rd party code
Generate PDBs for decompiled code
Step into any code and set breakpoints
Dynamic decompilation in Visual Studio
Includes .NET Reflector Desktop and Visual Studio extension
Note:
(a) i do not use this tool
(b) it's not free
(c) most likely, it uses reflection for at least some of what it does.
g.
DMW
All-Star
15943 Points
2353 Posts
Re: Can someone explain this reflection please
Dec 19, 2012 02:20 PM|LINK
No, the word "reflector" does not mean that at all.
A reflector is typically something the reflects (typically) energy, such as mirrors and the materials that are used in high visibility clothing.
Programmers, however, often talk about "reflecting over code" to mean using code to examine built code. There are lots of use cases for this, such as unit testing, locating imports (with MEF), validation, the routing engine in ASP.NET and ASP.NET MVC, assembly loading, serialization, WCF, and so on and so on.
We often use attributes in our code to facilitate this - such as [DataContract] and [OperationContract] in WCF, the [Imports] attribute in MEF, and the filter attributes on action methods in ASP.NET MVC.
The (no longer free) product, Reflector, is a tool that not only uses the Reflection APIs to examine code, but goes further by decompiling the actual IL blocks of the methods and reverse engineers these back into C#, VB.NET and other .NET languages. This is, of course, exceptionally useful as it lets you examine actual code. This is really handy when you want to understand, say, how lambda expressions and variable capture actually works, or what is really generated when you write iterator methods with yield return.
Dave