小编Thu*_*ter的帖子

访问其他类中的实现接口方法

我是C#新手,我正在尝试实现一个界面.我知道我不能将访问modiefiers放到接口方法上,那么如何在下面的'TestClass2'的公共静态'Create'方法中访问'TestValue'?我得到的错误是......

'TestClass1'不包含'TestValue'的定义,并且没有可以找到接受类型'TestClass1'的第一个参数的扩展方法'TestValue'

public interface IParent
{
    string TestValue { get; }
}

public class TestClass1 : IParent
{
    string IParent.TestValue
    {
        get { return "hello"; }
    }
}

public class TestClass2
{
    private string _testValue;

    public static TestClass2 Create(TestClass1 input)
    {
        TestClass2 output = new TestClass2();
        output._testValue = input.TestValue;
        return output;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# interface .net-4.0

2
推荐指数
1
解决办法
4676
查看次数

标签 统计

.net-4.0 ×1

c# ×1

interface ×1