C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Reflection - Obtaining Information about Methods at runtime


Posted Date: 07 Mar 2004    Resource Type: Articles    Category: .NET Framework
Author: ANITA MARY JOSEPHMember Level: Gold    
Rating: Points: 10




using System;
using System.Reflection;
class ThisClass
{
int firstValue, secondValue;
public int sum()
{
return firstValue + secondValue;
}
public void assign(int a, int b)
{
firstValue = a;
secondValue = b;
}
}
class ReflectionDemo
{
public static void Main()
{
Type t = typeof( ThisClass ); //get a Type object representing ThisClass.
Console.WriteLine( "Analyzing methods in " +t.Name );
Console.WriteLine( "\nMethods supported: \n" );
MethodInfo[] methods = t.GetMethods(); //Obtains List of methods.
foreach( MethodInfo m in methods )
{
Console.Write( ". "+m.ReturnType.Name+" "+m.Name + "(" );
ParameterInfo[] parameters = m.GetParameters(); //Obtains list of parameters.
for ( int i = 0; i < parameters.Length; i++ )
{
Console.Write( parameters[i].ParameterType.Name+ " " +parameters[i].Name );
if( i + 1 < parameters.Length)
Console.Write( ", " );
}
Console.WriteLine( ")" );
Console.WriteLine();
}
Console.ReadLine();
}
}


The code below is the second form of GetMethods():


MethodInfo[] methods = t.GetMethods( BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public );

DeclaredOnly =Retrieves only those methods defined by the specified class.Inherited methods
are not included.
Instance =Retrieves instance methods.
Public =Retrieves Public mathods.




Responses

Author: Sailash J Prakash    06 May 2004Member Level: Gold   Points : 0
the idea abt usage of reflection was really simple and g8, it will surely help the freshers as well as professionals.


Author: karthikeyan    25 May 2004Member Level: Bronze   Points : 0
i was really searching for an excellent way like this to expalin reflection to me,
any way thanks for dotnetspider


Author: critic    25 May 2004Member Level: Bronze   Points : 0
hello karthikeyan, it is good to see a feedback, but why do you have to submit it so many times ?


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Serializing array of objects
Previous Resource: Compare C# and C++
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use