如何从我的接口实例中读取基类属性

时间:2017-10-11 03:45:55

标签: c# wcf generics c#-4.0 reflection

 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的实例

1 个答案:

答案 0 :(得分:0)

界面ICommunication没有EndPoint,当你编写这行代码时:

ICommunication communication = new MyService();

communicationICommunication类型的引用,但它指向MyService的实例。因此,您可以将其强制转换为MyService然后访问它:

string ep = (communication as MyService).EndPoint;