There's an overload of Activator.CreateInstance that chooses the constructor to call based on the parameters you pass it.
How to instantiate a class using reflection and access class method (no interface)
-
22-07-2023 - |
문제
I have a C# solution with 3 projects.
- Windows Form Application
- Windows Library Module 1 (frmSearch, frmDetail)
- Windows Library Module 2 (frmSearch, frmDetail)
Module 1 and Module 2 has two forms each (as shown above). So what I want to do is from Module 2 I need to get Module1.frmDetail
, then Instantiate the class and then access its constructor so I can pass a parameter.
The problem is that I don't have an interface of Module1.frmDetail (don't know if I need it), here is my actual code from Module2.frmSearch:
// Use Reflection to load Module1.frmDetail
if(System.Reflection.Assembly.GetEntryAssembly() != null)
{
System.Reflection.Assembly a = System.Reflection.Assembly.Load("Module1.frmDetail");
if(a != null)
{
System.Type type = a.GetType("Module1.frmDetail");
var frm = Activator.CreateInstance(type);
// here i want to pass a parameter to the constructor???
}
}
Any clue on how to do this?
해결책
제휴하지 않습니다 StackOverflow