简体   繁体   中英

Return a class object from a dll C#

I have a dll in C# which returns a class object.

DLL code:

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;
        }
    }
}

I have another program "RunApp" in C# which has the same Person.cs class and tries to get the object from the above dll.

RunApp Code:

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();
        }
    }
}

After this, when I compile the "RunApp" code, I get an Error:

"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' ".

I want to send values from the Extract.dll to RunApp program. I want to use this dll at multiple programs.

Can anybody please help how to crack this problem?

A type is defined by its assembly. Two identical copies of Foo.Bar.SomeClass in two different assemblies are different types , and are not interchangeable - even if they have the same namespace etc.

You should reference the library, and re-use the type from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM