How to create an instance of the internal constructor with parameters using Reflection?

Harikrishnan

I have a different scenario. I need to create instance of a class which is public but having all its constructors as internal. The class has no default constructor.

I tried the below ways, but it didn't work.

Activator.CreateInstance(typeof(ClassName));
Activator.CreateInstance(typeof(ClassName), nonpublic:true);
Activator.CreateInstance(typeof(ClassName),true);
Activator.CreateInstance(typeof(ClassName), new object[]{double,bool});

I also tried this, but ended up with System.MissingMethodException.

var constructors = typeof(ClassName).GetConstructors();
foreach(var ctor in constructors)
    ctor.Invoke(new object[] {double, bool});

I am not able to use BindingFlags in Xamrarin. I am stuck, does anyone have a solution for this?

Harikrishnan

I found the solution by myself. Here is what I did.

ConstructorInfo info = typeof(ClassName).GetTypeInfo().DeclaredConstructors.First();
ClassName e = info.Invoke(new object[] { parameters }) as ClassName;

I hope this might help someone. Cheers:)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create an object instance of class with internal constructor via reflection?

From Java

How to create instance by interface using reflection?

From Dev

Using reflection to create an instance

From Dev

Java reflection create instance of unknown class and constructor

From Dev

Using reflection in Java to create a new instance?

From Dev

Create instance of List using reflection c#

From Dev

How to create constructor with optional parameters?

From Dev

How to create the object if constructor for the class is internal

From Dev

How to get default constructor parameter using reflection?

From Dev

How create button using reflection?

From Dev

How can I get a constructor's parameters via reflection in Dart?

From Dev

How can I get a constructor's parameters via reflection in Dart?

From Dev

Reflection: create an instance -java

From Dev

Copy constructor using reflection

From Dev

Java 8: create new instance of class with generics using reflection

From Dev

How to invoke an Android Dalvik internal method using Java reflection?

From Dev

How to create slice of struct using reflection?

From Dev

How to create protobuf instances using java reflection?

From Dev

How to create a property on a dynamic object using reflection

From Dev

instantiate object with reflection using constructor

From Dev

Mockito is unable to create instance using @InjectMocks because of Static class in constructor

From Dev

How to add an object to a generic list property of an instance of a class using reflection

From Dev

How to proper generify a method which returns an instance using Reflection API?

From Dev

How to find the concrete type of interface instance when using reflection?

From Dev

How to distinguish between enclosing and nested type parameters using Reflection in .NET?

From Dev

How can I create an instance of class without invoking constructor of this class?

From Dev

How to create instance of subclass with constructor from super class

From Dev

How do I create an instance of a inner class in the outer class constructor

From Dev

Objective-C: How to create object instance with constructor that references arrays

Related Related

  1. 1

    How to create an object instance of class with internal constructor via reflection?

  2. 2

    How to create instance by interface using reflection?

  3. 3

    Using reflection to create an instance

  4. 4

    Java reflection create instance of unknown class and constructor

  5. 5

    Using reflection in Java to create a new instance?

  6. 6

    Create instance of List using reflection c#

  7. 7

    How to create constructor with optional parameters?

  8. 8

    How to create the object if constructor for the class is internal

  9. 9

    How to get default constructor parameter using reflection?

  10. 10

    How create button using reflection?

  11. 11

    How can I get a constructor's parameters via reflection in Dart?

  12. 12

    How can I get a constructor's parameters via reflection in Dart?

  13. 13

    Reflection: create an instance -java

  14. 14

    Copy constructor using reflection

  15. 15

    Java 8: create new instance of class with generics using reflection

  16. 16

    How to invoke an Android Dalvik internal method using Java reflection?

  17. 17

    How to create slice of struct using reflection?

  18. 18

    How to create protobuf instances using java reflection?

  19. 19

    How to create a property on a dynamic object using reflection

  20. 20

    instantiate object with reflection using constructor

  21. 21

    Mockito is unable to create instance using @InjectMocks because of Static class in constructor

  22. 22

    How to add an object to a generic list property of an instance of a class using reflection

  23. 23

    How to proper generify a method which returns an instance using Reflection API?

  24. 24

    How to find the concrete type of interface instance when using reflection?

  25. 25

    How to distinguish between enclosing and nested type parameters using Reflection in .NET?

  26. 26

    How can I create an instance of class without invoking constructor of this class?

  27. 27

    How to create instance of subclass with constructor from super class

  28. 28

    How do I create an instance of a inner class in the outer class constructor

  29. 29

    Objective-C: How to create object instance with constructor that references arrays

HotTag

Archive