2012-06-18 36 views
2

我在C#中有一個dll,它返回一個類對象。從dll中返回一個類對象C#

DLL的代碼:

Person.cs:

namespace Extract 
{ 
    public class Person 
    { 
     public string name; 
     public string address; 
     public int age; 
     public int salary; 
    } 
} 

的Class1.cs

namespace Extract 
{ 
    public class MClass 
    { 
     public static Person GetPerson() 
     { 
      Person p = new Person(); 
      p.name = "Deepak"; 
      p.address = "Bangalore"; 
      p.age = 30; 
      p.salary = 20000; 
      return p; 
     } 
    } 
} 

我在C#中的另一個程序 「RunApp」 具有相同的人.cs類並嘗試從上面的dll獲取對象。

RunApp代碼:

Person.cs:

namespace Extract 
{ 
    public class Person 
    { 
     public string name; 
     public string address; 
     public int age; 
     public int salary; 
    } 
} 

Form1.cs中:

namespace Ex 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      Person mem = MClass.GetPerson(); 
     } 
    } 
} 

在此之後,當我編譯 「RunApp」 的代碼,我得到一個錯誤:

"Cannot implicitly convert type 'Extract.Person' to 'Ex.Person' ". I tried changing the namespace of the "RunApp" code from 'Ex' to 'Extract', but too the same Error: "Cannot implicitly convert type 'Extract.Person' to 'Extract.Person' ".

我想將Extract.dll中的值發送到RunApp程序。我想在多個程序中使用這個DLL。

任何人都可以請幫助如何解決這個問題?

+0

與這個問題無關,但是:不建議使用公共字段...我強烈建議您在每個結尾添加「{get; set;}」 –

回答

7

一種類型是通過其組裝定義爲。在兩個不同的組件Foo.Bar.SomeClass兩個完全相同的副本是不同的類型,並且是不可互換的 - 即使它們具有相同的命名空間等

你應該參考庫,並重新使用從那裏類型。