-11

Refer this...

http://docs.oracle.com/javase/tutorial/reflect/index.html or What is reflection and why is it useful?

Is there anything like this available in .Net platform?

Community
  • 1
  • 1
Krunal
  • 2,967
  • 8
  • 45
  • 101
  • 3
    Maybe [`System.Reflection.*`](http://msdn.microsoft.com/library/system.reflection.aspx)? – Steve B Oct 05 '12 at 12:22
  • 1
    Yes, it is called Reflection. What do you want to know about it? – Albin Sunnanbo Oct 05 '12 at 12:22
  • Any tutorial how to use it in .Net? – Krunal Oct 05 '12 at 12:23
  • 2
    You could have easily searched this on Google and found the answer yourself. Forgive me to say that I find you didn't put any effort in this ... Google: ".NET Reflection" @Jeff: thanks for the delete-answer tip :) – Abbas Oct 05 '12 at 12:29

1 Answers1

2

Reflection provides objects (of type Type) that encapsulate assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties

I have Used reflection for dynamically load assembly and display its forms with the help of Reflection.

Step 1: Loading an assembly form the specified path.

string path = Directory.GetCurrentDirectory() + @"\dynamicdll.dll";
try
{
    asm = Assembly.LoadFrom(path);
}
catch (Exception)
{
}

Step 2: Getting all forms of an assembly dynamically & adding them to the list type.

List<Type> FormsToCall = new List<Type>();
Type[] types = asm.GetExportedTypes();
foreach (Type t in types)
{
    if (t.BaseType.Name == "Form")
        FormsToCall.Add(t);
}

Step 3:

int FormCnt = 0;
Type ToCall;
while (FormCnt < FormsToCall.Count)
{
     ToCall = FormsToCall[FormCnt];
     //Creates an instance of the specified type using the constructor that best matches the specified parameters.
     object ibaseObject = Activator.CreateInstance(ToCall);
     Form ToRun = ibaseObject as Form;
     try
     {
          dr = ToRun.ShowDialog();
          if (dr == DialogResult.Cancel)
          {
             cancelPressed = true;
             break;
          }
          else if (dr == DialogResult.Retry)
          {
             FormCnt--;
             continue;
          }
     }
     catch (Exception)
     {
     }
     FormCnt++;
}
Rahul
  • 591
  • 1
  • 7
  • 21