Вопрос

I have a C# solution with 3 projects.

  1. Windows Form Application
  2. Windows Library Module 1 (frmSearch, frmDetail)
  3. 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?

Это было полезно?

Решение

There's an overload of Activator.CreateInstance that chooses the constructor to call based on the parameters you pass it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top