reflection - C# create constructor while running -


i started read reflection , wonder, there way create constructor while program running. example: class c , check if c got empty constructor, if not, create on , use create instance. constructor might have parameters too.

how can that? thank you

in .net world, need know quite things.

  1. when class doens't have constructor, compiler adds default public 1 without parameters.

that means when have following class:

public class person {     public int age     {         { return 10; }     } } 

it can instantiated like:

class program {     static void main(string[] args)     {         var person = new person();         console.writeline(person.age);     } } 

this produce following output:

enter image description here

  1. as create constructor, default public, parameterless 1 not added compiler.

this means when change person class following:

public class person {     private person() { }      public int age     {         { return 10; }     } } 

you're not able instantiate class using new() keyword:

var person = new person(); 

this result in compile error:

enter image description here

  1. through reflection, can construct object event if constructor private and/or contain parameters.

that being said, question unclear because never need create constructor @ runtime, because of following:

  • a class without constructor has constructor added compiler , can constructed through reflection.
  • a class constructor can constructed through reflection.
  • a class in-accesible constructed can constructed through reflection.

now, let's see how construct classes using reflection using 3 scenario's above:

  • you have class without constructor.

    public class person {     public int age     {         { return 10; }     } } 

    your instance can created like:

    var instance = (person)activator.createinstance(typeof(person)); 
  • you have class public constructor

    public class person {     public person() { }      public int age     {         { return 10; }     } } 

    your instance can created in same way above, nothing special needed.

    var instance = (person)activator.createinstance(typeof(person)); 
  • you have class private constructor

    public class person {     private person() { }      public int age     {         { return 10; }     } } 

    now instance cannot created in such easy way. first, need constructorinfo:

    constructorinfo c = typeof(person).getconstructor(bindingflags.nonpublic | bindingflags.instance, null, new type[] {  }, null); 

    with code above, requesting information constructors not public (bindingflags.nonpublic).

    after have retrieved it, can construct object like:

    var instance = (person)c.invoke(new object[] { }); 

let's make bit more difficult following scenario's:

  • you have class public constructor accepts parameters:

    public class person {     public person(int age)     {         age = age;     }      public readonly int age; } 

    the creation still quite easy:

    var instance = (person)activator.createinstance(typeof(person), 15); 

    you need pass parameters in same way defined in constructor.

  • you have class private constructor accepts parameters:

    public class person {     public person(int age)     {         age = age;     }      public readonly int age; } 

    again, we'll need constructorinfo object, need specify types of parameters constructor accepting. in example above, integer:

    constructorinfo c = typeof(person).getconstructor(bindingflags.nonpublic | bindingflags.instance, null, new[] { typeof(int) }, null); 

    now, can create object , last parameter specify arguments construct object.

    var instance = (person)c.invoke(new object[] { 15 }); 

so, long post reflection hope mde things clear. @ least, should clear right why don't need add constructor @ runtime in able invoke object.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -