•
Reflection
is used in the process of obtaining type information at runtime. The classes
that give access to the metadata of a running program are in System.Reflection.
•
Reflections
is generally used for:
–
Viewing
metadata: allows viewing examining attribute information from the code at
runtime.
–
Performing
type discovery: allows examining the various types in an assembly and
instantiate those types.
–
Late
binding to methods and properties: allows the developer to call properties and
methods on dynamically instantiated objects using discovery.
–
Reflection
emit: allows creating new types at runtime and then to use those types to
perform tasks.
Using System;
using System.Reflection;
public class MyClass
{
public virtual int AddNumb(int
numb1,int numb2)
{
int result = numb1 + numb2;
return result;
}
}
class MyMainClass
{
public static int Main()
{
Console.WriteLine
("\nReflection.MethodInfo");
// Create MyClass object
MyClass myClassObj = new
MyClass();
// Get the Type information.
Type myTypeObj =
myClassObj.GetType();
// Get Method Information.
MethodInfo myMethodInfo =
myTypeObj.GetMethod("AddNumb");
object[] mParam = new object[]
{5, 10};
// Get and display the Invoke method.
Console.Write("\nFirst
method - " + myTypeObj.FullName + " returns " +
myMethodInfo.Invoke(myClassObj, mParam) + "\n");
}
}
______________________________________________________________________
Reach us At: - 0120-4029000; 0120-4029024; 0120-4029025,
0120-4029027; 0120-4029029
Mbl: 9953584548
No comments:
Post a Comment