通过接口引用调用,将接口驱动方法从一个项目调用到另一个项目 [英] Calling interface driven method from one project to another project by calling through Interface references
问题描述
命名空间Project1
{
public interface IInterface1
{
void Method();
}
public interface IInterface2:IInterface1
{
void Method();
void Method2();
}
//这里我们实现了获取和发送数据到数据库的方法
公共类GetSentData:IInterface1
$
{
void Method();
void Method2();
}
}
注意:我们正在制作.dll上面提到了接口和类。
//这是另一种解决方案
//我将Project1的引用添加到project2中
//这里我们要通过使用接口驱动类来调用上面的类方法
//喜欢这样但是我得到错误:对象是未设置为对象实例
命名空间Project2
{
公共类Acess
{
IInterface1 myinterface = new GetSentData();
myinterface.Method();
}
}
//请提出您的宝贵答案
namespace Project1
{
public interface IInterface1
{
void Method();
}
public interface IInterface2 : IInterface1
{
void Method();
void Method2();
}
// Here we are implementing methods for get and sent data to database
public class GetSentData: IInterface1
{
void Method();
void Method2();
}
}
note: We are making a .dll of above mentions interfaces and class.
//This is a another solution
//I add reference of Project1 into project2
//Here we want to call above class method by using the interface driven class
//Like This but am getting error as: Object is not set to an instance of object
namespace Project2
{
public class Acess
{
IInterface1 myinterface = new GetSentData();
myinterface.Method();
}
}
//Please Suggest with your valuable answers
推荐答案
您好Shubha Mondal,
Hi Shubha Mondal,
感谢您在此发帖。
我举一个简单的例子来测试。您可以从project1类库中添加dll的引用。
I make a simple example to test. You could add reference of the dll from project1 class library.
1。使用以下代码创建一个类库。
1. Create a classlibrary with the following code.
namespace ClassLibrary1
{
public interface IInterface1
{
void Method();
}
public interface IInterface2 : IInterface1
{
void Method2();
}
// Here we are implementing methods for get and sent data to database
public class GetSentData : IInterface2
{
public void Method()
{
Console.WriteLine("Interface1 Method");
}
public void Method2()
{
Console.WriteLine("Interface2 Method2");
}
}
}
2。从ClassLibrary1添加dll的引用。
2. Add reference of the dll from ClassLibrary1.
3。这是project2的一个简单用法。
3. Here is a simple use of project2.
namespace interface_Test
{
class Program
{
static void Main(string[] args)
{
GetSentData getData = new GetSentData();
getData.Method();
getData.Method2();
}
}
}
最诚挚的问候,
Best Regards,
Wendy
这篇关于通过接口引用调用,将接口驱动方法从一个项目调用到另一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!