2014-12-25 8 views
0

에서호출 대상에 의해 예외가 발생했습니다. C#을

C# Get form names of project A from Project B

에 주어진에 따라 나는 프로젝트 B로 프로젝트 A에서 모든 "양식 이름"을 얻고 난 @jerry의 대답을 사용하고 있습니다.

내 코드가 var f = (Form)emptyCtor.Invoke(new object[] { }); 행에 도달했을 때 Exception이 호출 대상에 의해 throw되었습니다. Error.

아래 내가 오류 이유를 검색했지만 실패했다

try 
{ 
    Assembly projectA = Assembly.Load("ProjectA"); // replace with actual ProjectA name 
    // despite all Microsoft's dire warnings about loading from a simple name, 
    // you should be fine here as long as you don't have multiple versions of ProjectA 
    // floating around 

    foreach (Type t in projectA.GetTypes()) 
    { 
     if (t.BaseType == typeof(Form)) 
     { 
      var emptyCtor = t.GetConstructor(Type.EmptyTypes); 
      if (emptyCtor != null) 
      { 
       var f = (Form)emptyCtor.Invoke(new object[] { }); 
       // t.FullName will help distinguish the unwanted entries and 
       // possibly later ignore them 
       string formItem = t.FullName + " // " + f.Text + " // " + f.Name; 
       checkedListBox1.Items.Add(formItem); 
      } 
     } 
    } 
} 
catch(Exception err) 
{ 
    // log exception 
} 

전체 코드를입니다. 내가

편집 worng하고 나에게 우리를 제안 해주십시오 : 아래

는 오류 정보

이미지 1 개 Error1

이미지 2

Err2

+1

오류가 발생하면 InnerException이 발생합니까? – dewaffled

+0

InnerException에 StackTrace가 있습니다 - 검사하여 무엇이 잘못되었는지 더 잘 이해할 수 있습니다 – J0HN

+2

@ J0HN InnerException 스택 추적은 예외가 'FrmMachineryType' 생성자에 의해 처리되었음을 나타냅니다. 이 어셈블리에 대한 디버그 정보 ('.pdb' 파일)가 있고 스택 추적에 예외를 throw하는 행 번호가 포함되어 있어야합니다. 소스 코드에 액세스 할 수 있으면 오류의 원인을 확인할 수 있습니다. 아마도 폼 생성자는 생성되지 않은 정적 객체에 접근하려 할 것입니다. – dewaffled

답변

2

이 시도 :

BTW
var f = (Form)emptyCtor.Invoke(null); 

, 당신이 달성하려고?

2
private void childclick(object sender, EventArgs e) 
{ 
    DataTable dtTrans = new DataTable(); 
    dtTrans = Db.bindData("SELECT frm_Code FROM tbl_MST_SubMnu WHERE frm_Name='" + sender.ToString() + "'"); 

    Assembly frmAssembly = Assembly.LoadFile(Application.ExecutablePath); 
    foreach (Type type in frmAssembly.GetTypes()) 
    { 
     if (type.BaseType == typeof(Form)) 
     { 
      if (type.Name == dtTrans.Rows[0][0].ToString()) 
      { 
       Form frmshow = (Form)frmAssembly.CreateInstance(type.ToString()); 

       frmshow.Show(); 
      } 
     } 
    } 
} 
관련 문제