【问题标题】:How to read base class properties from my interface instance如何从我的接口实例中读取基类属性
【发布时间】:2017-10-11 03:45:54
【问题描述】:
 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 Generic 类

注意:我们有多个服务类。有没有办法从我的 ICommunication 中获取 ClientBase 的实例

【问题讨论】:

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


    【解决方案1】:

    接口ICommunication没有EndPoint而当你写这行代码时:

    ICommunication communication = new MyService();
    

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

    string ep = (communication as MyService).EndPoint;
    

    【讨论】:

    • 我忘记放这个了。我们有多个服务类。有没有办法从我的 ICommunication 获取 ClientBase 的实例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 2015-05-22
    相关资源
    最近更新 更多