What is reflection.?

2 views
Skip to first unread message

Prince P Y

unread,
Nov 7, 2005, 11:44:16 AM11/7/05
to Techdot...@googlegroups.com

What is reflection actually.?

Give come c# code which work like eval in javascript?

 

Regards,

Prince P Y [B.E I.T],

S/W Engineer,

REACH Technologies,

#621, 5th Main,

OMBR Layout,

Bangalore -43

www.reach-tech.com

Mob: 91-944862035

Res: 080-55369104

Off: 080-56996111/2/3 - Ext: 227

 

Current Res Address:

24/6, 3rd Cross, Sri Lakshmi Residency,

St. Johns Orthodox Church Road,

A Narayanapura, Nagappareddy Layout,

Near K.R.Puram Railway station,

Bangalore – 16, India

 

Shivprasad Koirala

unread,
Nov 8, 2005, 5:33:50 AM11/8/05
to .NetIndia
with reflection you can browse through meta-data and also invoke
method dynamically
-------
Regards ,
C#, VB.NET , SQL SERVER , UML , DESIGN Patterns Interview question book
http://www.geocities.com/dotnetinterviews/
My Interview Blog
http://spaces.msn.com/members/dotnetinterviews/

BabuLives

unread,
Nov 8, 2005, 6:06:12 AM11/8/05
to .NetIndia
Reflection:

All .NET compilers produce metadata about the types defined in the
modules they produce. This metadata is packaged along with the module
(modules in turn are packaged together in assemblies), and can be
accessed by a mechanism called reflection. The System.Reflection
namespace contains classes that can be used to interrogate the types
for a module/assembly.


Program:

using System;
using System.Reflection;

namespace ConsoleReflection
{
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Assembly ass=Assembly.LoadFrom(@"C:\libraries.dll");
//Loading the dll
Type[] ty=ass.GetTypes();
//Gets all the types that is in the assembly libraries.dll
object lib=ass.CreateInstance(ty[0].ToString());
//Creating instance for the type declared in the assembly
MethodInfo[] meth=ty[0].GetMethods();
//Getting alll the methods in the assembly
for(int i=0;i<meth.Length;i++)
{
if(meth[i].Name=="sum")
{
object[] param={1,2};
//Creating Parameter to invoke the function
object ans=meth[i].Invoke(lib,param);
//Invoking a method "sum" in the dll using Reflection
int sum=int.Parse(ans.ToString());
Console.WriteLine("The output Calculated using
Reflection is");
Console.Write(sum.ToString());
}
}
Console.Read();
}
}
}

OUTPUT:

The output Calculated using Reflection is
4

Libraries:(A Class Library Project)

using System;

namespace libraries
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public int sum(int a,int b)
{
return a+b;
}
}
}

Regards,
Satheesh

Juneja Tarun

unread,
Nov 8, 2005, 6:22:49 AM11/8/05
to .NetIndia
.Net Reflection is a very powerful mech which provide the functionality
so that you can
load
investigate
assemblies
@ run time.

you can find the constructor,function of a assembly
even you can find functions arguments ant type

System.Reflection
assembly
Bye

Girivasan

unread,
Nov 8, 2005, 9:02:24 AM11/8/05
to Techdot...@googlegroups.com
Hi,
 
 see the attachement for reflection.
 
Regards,
Girivasan.V


 
On 11/7/05, Prince P Y <princ...@gmail.com> wrote:

What is reflection actually.?

Give come c# code which work like eval in javascript?

 

Regards,

Prince P Y [B.E I.T],

S/W Engineer,

REACH Technologies ,

#621, 5th Main,

OMBR Layout,

Bangalore -43

www.reach-tech.com

Mob: 91-944862035

Res: 080-55369104

Off: 080-56996111/2 /3 - Ext: 227

Reflection.doc
Reply all
Reply to author
Forward
0 new messages