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通用类
注意:我们有多个服务类。有没有办法从我的ICommunication获取ClientBase的实例
答案 0 :(得分:0)
界面ICommunication
没有EndPoint
,当你编写这行代码时:
ICommunication communication = new MyService();
communication
是ICommunication
类型的引用,但它指向MyService
的实例。因此,您可以将其强制转换为MyService
然后访问它:
string ep = (communication as MyService).EndPoint;