2017-10-11 66 views
0
public interface IMyService 
{ 
    void GetValue(); 
} 

public class MyService : ClientBase<IMyService>, IMyService 
{ 
    public MyService() 
    { 
     EndPoint = "Test"; 
    } 
    public void GetValue() 
    { 

    } 
} 
public interface ICommunication 
{ 
    void Start(); 
} 

public class ClientBase<T> : ICommunication 
{ 
    public string EndPoint { get; set; } 
    public void Start() 
    { 
    } 
} 

我的測試項目中讀取基類的屬性如何從我的接口實例

[TestClass] 
public class UnitTest1 
{ 
    [TestMethod] 
    public void TestMethod1() 
    { 
     ICommunication communication = new MyService(); 
    } 
} 

如何訪問從通信對象的EndPoint屬性?

我的目標是從ICommunication實例中讀取EndPoint的值。如何轉換的ICommunication接口ClientBase泛型類

注:我們有多個服務classes.Is有沒有辦法從我ICommunication

回答

0

接口ICommunication得到ClientBase的實例沒有EndPoint,當你寫這行代碼:

ICommunication communication = new MyService(); 

communicationICommunication類型的參考,但它指向的MyService一個實例。因此,你可以這樣做,把它投到MyService,然後訪問它:

string ep = (communication as MyService).EndPoint; 
+0

我忘了把這一個。我們有多個服務類。有沒有辦法從我的ICommunication中獲得ClientBase 的實例 –