在不同的程序集中实现接口 [英] Implementing interface in different assemblies

查看:72
本文介绍了在不同的程序集中实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的界面和类



I have an interface and class like this

public interface ICustomerEx
{
   void Load(DataRow row);
}

public class Customer:ICustomerEx
{
   public string Name{get;set;}
   public string Address{get;set;}

   void Load(DataRow row)
   {
	Name = (string)row["name"];
	Address = (string)row["address"];
   }
}







现在我把它建成一个班级库,并添加到另一个项目作为参考。

在该项目中有一个名为UiCustomer的类,它使用相同的参考接口实现ICustomerEx

在这个类中它有自己的属性并从其加载方法加载该属性。






Now I'm building this as a class library and add to another project as a reference.
In that project there's a class called UiCustomer that implements from the same refereced interface ICustomerEx
In this class it has its own property and load that property from its load method.

public class UiCustomer:ICustomerEx
{
   public string Telephone{get;set;}

   void Load(DataRow row)
   {
	Telephone=(string)row["tele"];
   }
}





现在有没有办法实现我的第一个类(构建为类库) )使用类似的依赖注入加载它自己的属性后加载Ui项目属性的加载方法。



例如。





Now is there any way to implement my first class's(that build as a class library) Load method to load Ui project's properties after loading it's own properties by using like Dependency Injection.

eg.

public class Customer:ICustomerEx
{
   public string Name{get;set;}
   public string Address{get;set;}

   void Load(DataRow row)
   {
	Name = (string)row["name"];
	Address = (string)row["address"];

	//Call load methods in other places that Interface implemented
   }
}

推荐答案

如果有额外的财产/字段

What about having an additional property/field
ICustomerEx _Inner;



其值可以注入构造函数,或者在构造之后设置。

然后在你的Load中( )方法,你可以调用它的Load方法:


whose value could be injected into the constructor, or set after construction.
Then in your Load() method, you could call its Load method:

if (_Inner != null)
{
    _Inner.Load();
}


这篇关于在不同的程序集中实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆