Click here to Skip to main content
15,889,354 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
I want to understand the concept of Reflection in ASP.Net c# and want to know how to implement Reflection API in ASP.Net ?
Posted

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Oct-12 22:04pm    
The only reasonable answer so far -- others went into a much ado about nothing. My 5.
--SA
Reflection provides objects (of type Type) that encapsulate 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. For more information, see Attributes.


For example, if you want to programmatically display all the methods of a class, you could do it like so:

C#
using System;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            var t = typeof(MyClass);

            foreach (var m in t.GetMethods())
            {
                Console.WriteLine(m.Name);
            }
            Console.ReadLine();
        }
    }


    public class MyClass
    {
        public int Add(int x, int y)
        {
            return x + y;
        }

        public int Subtract(int x, int y)
        {
            return x - y;
        }
    }
}



For More Information You Can Refer The Link : Reflection in .NET[^]
 
Share this answer
 
v2
Comments
surajfrommumbai 3-Oct-12 5:45am    
This is Crap Hemu. Its only small code which does not show practically how things are.
[no name] 3-Oct-12 6:22am    
I have just show you the basic concept for the Reflection.
If you want more information then i have also attached a link to MORE ABout Reflection.
Have nice day...
Mr. Meghal..
The answer given by hemu is good but not enough to get full confidence..
I think you should more explore about reflection because it is the most important topic i've learnt in my career..
Go ahead..
Best of luck..
 
Share this answer
 
Comments
Rajan Shuklan 2-Oct-12 5:42am    
Rather than commenting on a user.. I think you should suggest your own answer...!!
Dude GROW UP...!!
Dhruti Shah 2-Oct-12 5:44am    
Good Reply Mr Meghal Modi.
Rajan Shuklan 2-Oct-12 5:44am    
Thank you ..!! Member 8556965 :)
Meet Raval 2-Oct-12 5:46am    
Mr. Meghal, I've already explained that Reflection is very huge topic to understand.
And also you haven't asked the particular question about reflection..
I think you haven't browse google and reference websites carefully..
You should explore more before asking question..
And yes, I've already grown up Dude...
Rajan Shuklan 2-Oct-12 5:49am    
Whatever it is..!!! But why are you commenting on a user...!! If you dont have an answer than you should not have commented on Developer Hemant Patel..!! And from your reply you seem to be hurt..!! Dude Chill..!!
Ok Mr. Modi try this....

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace ReflectionTest
{
    class Program
    {
        private static int a = 5, b = 10, c = 20;

        static void Main(string[] args)
        {
            Console.WriteLine("a + b + c = " + (a + b + c));
            Console.WriteLine("Please enter the name of the variable that you wish to change:");
            string varName = Console.ReadLine();
            Type t = typeof(Program);
            FieldInfo fieldInfo = t.GetField(varName, BindingFlags.NonPublic | BindingFlags.Static);
            if(fieldInfo != null)
            {
                Console.WriteLine("The current value of " + fieldInfo.Name + " is " + fieldInfo.GetValue(null) + ". You may enter a new value now:");
                string newValue = Console.ReadLine();
                int newInt;
                if(int.TryParse(newValue, out newInt))
                {
                    fieldInfo.SetValue(null, newInt);
                    Console.WriteLine("a + b + c = " + (a + b + c));
                }
                Console.ReadKey();
            }
        }
    }
}




You can explore more on reflection..Please go to the following link...
http://csharp.net-tutorials.com/reflection/introduction/[^]


I think modi, you should learn from the links provided by JF2015.

And Developer Hemu Patel, Don't hurt my Ego...

Have a nice day...
 
Share this answer
 
Comments
Jaynav_Parikh 2-Oct-12 6:13am    
OK now here it is.. I was expecting the answer like this from you..
If hemant would have not hurt your ego you would not have submitted a proper answer..!!
Nice Work Hemant..!!
Genius Dude..!!
Mr. Modi you should refer the following link..
Reflection in .NET[^]


You will learn lot of thinks about reflection and i'm sure you will get enough confedence so that you can crack interview questions on Reflection..

And yes, don't rely only on Hemu Patel's answer...

Take it positively...
 
Share this answer
 
Comments
Jaynav_Parikh 2-Oct-12 6:18am    
Dude for your kind information.. The link you have provided was given by hemant before only...!!
This proves that you are following hemant rather giving answer explored by yourself..!!
This is Shamefull..!!
Meet Raval 2-Oct-12 6:23am    
Ohh Mr. Jaynav, I think a new learner should refer that link because it gives the best solution I've ever seen..
And yes, Mr. Jaynav..Knowledge is more important...Please consider the concept and knowledge provided in the link that i've given...Don't consider who gives this link..
Either me or hemu patel is not important..But the concept is important.
God bless you, jaynav....
Jaynav_Parikh 2-Oct-12 6:28am    
Thanx for your blessings bro..!! Knowledge is important but manners are also important..
You should first learn how to talk to people..
You are copying hemant patel that proves that you are just trying to improve your reputation and at the same time it also shows that you are not interested in knowledge you are just hungry for reputation...!!!
Meet Raval 2-Oct-12 6:35am    
No Mr. Jaynav...I already have good manners to talk to people..
My Major intension was just to guide Mr. Meghal Modi.. Why are you embrassing us??
And I'm not copying from Hemu patel..I've given solution what i've learnt about reflection through out my career..
I think you are more hungry so you are giving this type of answers to get publicity like Rakhi Sawant and Sherlyn Chopra in bollywood..
Again God bless you, Jaynav..
Jaynav_Parikh 2-Oct-12 6:40am    
Dude first of all its not embrassing its embarassing..!!
Secondly we all know that who is publisizing right now in front of everyone.. we all can see.. And its also clearly visible that that who is hungry for reputation..!!
The answer posted by Hemu is useless and impractical. Its copy pasted from some other place.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900