reflection in C#.net [Resolved]

Posted by Sudhakar_A under C# on 9/27/2013 | Points: 10 | Views : 2818 | Status : [Member] | Replies : 4
How to use reflection in C#??
pls provide some example.........




Responses

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You find the detail information in below link:-

http://www.codeproject.com/Articles/55710/Reflection-in-NET

Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Reflection
Reflection is the ability of a managed code to read its own metadata for the purpose of finding assemblies, modules and type information at runtime. In other words, reflection provides objects that encapsulate assemblies, modules and types. A program reflects on itself by extracting metadata from its assembly and using that metadata either to inform the user or to modify its own behavior.

Refer this link:
http://www.codeproject.com/Articles/17269/Reflection-in-C-Tutorial

This example shows how a string literal is used to find a variable with that same name.
using System;
using System.Reflection;

class Program
{
public static int _number = 7;
static void Main()
{
Type type = typeof(Program);
FieldInfo field = type.GetField("_number");
object temp = field.GetValue(null);
Console.WriteLine(temp);
}
}

Output

7


Reference: http://www.dotnetperls.com/reflection
http://www.tutorialspoint.com/csharp/csharp_reflection.htm

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down

Reflection allows you to write code that can inspect various aspects about the code itself.

It enables you to do simple things like:

Check the type of an object at runtime (simple calls to typeof() for example)

Inspect the Attributes of an object at runtime to change the behavior of a method (the various serialization methods in .NET)

To much more complicated tasks like:

Loading an assembly at runtime, finding a specific class, determining if it matches a given Interface, and invoking certain members dynamically.
The earlier is much more common usage. The later is helpful to developers working on plug-in architectures for their applications or people who want to swap assemblies at runtime depending on configuration changes.

Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
This example shows how to dynamically load assembly, how to create object instance, how to invoke method or how to get and set property value.
Create instance from assembly that is in your project References
The following examples create instances of DateTime class from the System assembly.

// create instance of class DateTime

DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime));

// create instance of DateTime, use constructor with parameters (year, month, day)
DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime),
new object[] { 2008, 7, 4 });


Create instance from dynamically loaded assembly
All the following examples try to access to sample class Calculator from Test.dll assembly. The calculator class can be defined like this.

namespace Test

{
public class Calculator
{
public Calculator() { ... }
private double _number;
public double Number { get { ... } set { ... } }
public void Clear() { ... }
private void DoClear() { ... }
public double Add(double number) { ... }
public static double Pi { ... }
public static double GetPi() { ... }
}
}

Examples of using reflection to load the Test.dll assembly, to create instance of the Calculator class and to access its members (public/private, instance/static).

// dynamically load assembly from file Test.dll

Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll");


// get type of class Calculator from just loaded assembly

Type calcType = testAssembly.GetType("Test.Calculator");

// create instance of class Calculator

object calcInstance = Activator.CreateInstance(calcType);


// get info about property: public double Number
PropertyInfo numberPropertyInfo = calcType.GetProperty("Number");


// get value of property: public double Number

double value = (double)numberPropertyInfo.GetValue(calcInstance, null);


// set value of property: public double Number
numberPropertyInfo.SetValue(calcInstance, 10.0, null);


// get info about static property: public static double Pi
PropertyInfo piPropertyInfo = calcType.GetProperty("Pi");


// get value of static property: public static double Pi
double piValue = (double)piPropertyInfo.GetValue(null, null);


// invoke public instance method: public void Clear()

calcType.InvokeMember("Clear",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, calcInstance, null);


// invoke private instance method: private void DoClear()

calcType.InvokeMember("DoClear",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
null, calcInstance, null);


// invoke public instance method: public double Add(double number)

double value = (double)calcType.InvokeMember("Add",
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, calcInstance, new object[] { 20.0 });


// invoke public static method: public static double GetPi()

double piValue = (double)calcType.InvokeMember("GetPi",
BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
null, null, null);

// get value of private field: private double _number

double value = (double)calcType.InvokeMember("_number",
BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
null, calcInstance, null);


Reference:
http://www.codeguru.com/csharp/csharp/cs_misc/reflection/article.php/c4257/An-Introduction-to-Reflection-in-C.htm

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response