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
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
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
you can find the constructor,function of a assembly
even you can find functions arguments ant type
System.Reflection
assembly
Bye
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
Mob: 91-944862035
Res: 080-55369104
Off: 080-56996111/2 /3 - Ext: 227