Library tutorials & articles
Reflection in C#
- Introduction
- An Example
An Example
One very useful feature related to reflection is the ability
to create object dynamically and call methods on them. You can specify which
class you want a Type object, or by giving the name of an assembly and a class
as string, so this makes it possible to get the name of a class from the user
and create an object of the appropriate type. You can then interact with the
new object just as if you’d created it with new, and use reflection to find out
just you’re dealing with and what it can do.
Following C# code will demonstrate the reflection capabilities.
namespace
reflect
{
using System ;
using System.Reflection ;
publicclass ClassA
{
publicstaticint
Main ( string[] kami )
{
Type t = typeof( test ) ;
Console.WriteLine ( "Type of class: " + t ) ;
Console.WriteLine ( "Namespace: " + t.Namespace ) ;
ConstructorInfo[] ci = t.GetConstructors( );
Console.WriteLine( "Constructors are:" ) ;
foreach( ConstructorInfo i in ci )
{
Console.WriteLine( i ) ;
}
PropertyInfo[] pi = t.GetProperties( );
Console.WriteLine( "Properties are:" ) ;
foreach( PropertyInfo i in
pi )
{
Console.WriteLine( i ) ;
}
MethodInfo[] mi = t.GetMethods( ) ;
Console.WriteLine( "Methods are:" ) ;
foreach( MethodInfo i in mi )
{
Console.WriteLine( "Name: " + i.Name ) ;
ParameterInfo[] pif = i.GetParameters () ;
foreach ( ParameterInfo p
in pif )
{
Console.WriteLine("Type:
" + p.ParameterType + " parameter name: " + p.Name ) ;
}
}
return 0 ;
}
}
publicclass test
{
int i ;
int prop;
publicint Prop
{
get
{
return prop ;
}
set
{
prop
= value ;
}
}
public test()
{
}
public test ( int
x )
{
i = x ;
}
publicint funcA
( int x )
{
Console.WriteLine ( x ) ;
return 2 * x ;
}
}
}
Related articles
Related discussion
-
C# video Editing/rendering
by pkuchaliya (0 replies)
-
How to Fill DataSet with more records (around 1 lakh) in a faster way
by Jayaram P (0 replies)
-
Can't print on the network with MSADESS ??
by anatha1 (2 replies)
-
Very Urgent regarding deleting the images from a folder
by Nanosteps (6 replies)
-
DataGridViewComboBoxColumn not showing values
by sachinkalse (0 replies)
Related podcasts
-
Object-Oriented Programming in Ruby
In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...
Yeh,there are many article which drive one concept or another .But the most fascinating thing is logics.
Nobody is born whith all in it's mind(not even the mind code that is hash distribution,it's work well as we know) we obtain a sum of knowledge by education,and other tends to be our proper discover,The way in which logic states about sets proofs ,df,theo,corrl,meta's and so one.We look for many model of thinking ,some of them already implemented,other in reasearch phase.Writing about proper transformation is not something wrong,and become obvious that simple ideea has to be presented in
theortical forms,and these theoretical forms should became a standard of prezentation in order to avoid affirmative.That is why we need to build logic theories and present them as should be presented.
Or you can use upper like expresion, doesn't bother important is communication
Is Microsoft the inventor of reflection ?
Excelent article.And well apreciated that you share your thoughts.
Ctin Negut
Lame, this is stolen from the docs, even down to the poor formating micro$oft uses
This thread is for discussions of Reflection in C#.