gpt4 book ai didi

c# - 我可以在接口(interface)方法中检索接口(interface)类型吗?

转载 作者:行者123 更新时间:2023-11-30 13:58:08 26 4
gpt4 key购买 nike

如何确定从中调用方法 DoSomething() 的底层接口(interface)?附加问题:我是否已经确定了 MyClass 构造函数中的底层接口(interface)?我假设不是,因为它在实例化时是未知的,对吗?

编辑:我不是在寻找显式接口(interface)实现,而是在寻找确定底层接口(interface)的不同方法。

public interface ITest
{
void DoSomething();
//....more methods
}

public interface IDecoy
{
void DoSomething();
//...more methods
}

public class MyClass : ITest, IDecoy
{
public void DoSomething()
{
//Question: How can I determine the underlying interface that called this method?
//at one time it is ITest, at another IDecoy. How can I figure out which one at each time?
}
}

public class Test
{
public Test()
{
ITest myClassInstance1 = new MyClass();
IDecoy myClassInstance2 = new MyClass();

myClassInstance1.DoSomething();
myClassInstance2.DoSomething();
}
}

最佳答案

public class MyClass : ITest, IDecoy
{
void ITest.DoSomething()
{
//called with ITest
}
void IDecoy.DoSomething()
{
//called with IDecoy
}
}

关于c# - 我可以在接口(interface)方法中检索接口(interface)类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18203446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com