Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现两个接口同名函数 #679

Open
zhouwenkan opened this issue Mar 24, 2022 · 3 comments
Open

实现两个接口同名函数 #679

zhouwenkan opened this issue Mar 24, 2022 · 3 comments

Comments

@zhouwenkan
Copy link

public interface I1
{
    void Test();
}

public interface I2
{
    void Test();
}

public class C1 : I1, I2
{
    void I1.Test()
    {
        Debug.Log("I1.Test");
    }

    void I2.Test()
    {
        Debug.Log("I2.Test");
    }
}

var s = new C1();
((I1)s).Test();
((I2)s).Test();

NullReferenceException: Object reference not set to an instance of an object.
Without useful stack information.

@liiir1985
Copy link
Collaborator

运行环境和ILRuntime版本分别是什么?

@zhouwenkan
Copy link
Author

unity 2020.3.29f1
ILRuntime 2.0.2

@RainKeyBoy
Copy link

RainKeyBoy commented Apr 7, 2023

  • 用例1:正常打出Log的测试用例
public static class Main
{
    public static void Test()
    {
        InterfaceUnit interfaceUnit = new InterfaceUnit();
        
        ITest test = interfaceUnit as ITest;
        
        test.Test();
    }
}

public interface ITest
{
    public void Test()
    {
        Debug.Log("InterfaceUnit");
    }
}

public class InterfaceUnit : ITest
{
    void Test()
    {
        Debug.Log("InterfaceUnit");
    }
}
  • 用例2:异常的测试用例(注释掉子类的实现)
public static class Main
{
    public static void Test()
    {
        InterfaceUnit interfaceUnit = new InterfaceUnit();
        
        ITest test = interfaceUnit as ITest;
        
        test.Test();
    }
}

public interface ITest
{
    public void Test()
    {
        Debug.Log("InterfaceUnit");
    }
}

public class InterfaceUnit : ITest
{
    // public void Test()
    // {
    //     Debug.Log("InterfaceUnit");
    // }
}
  • 用例3:异常的测试用例(函数声明使用ITest.Test()方式)
public static class Main
{
    public static void Test()
    {
        InterfaceUnit interfaceUnit = new InterfaceUnit();
        
        ITest test = interfaceUnit as ITest;
        
        test.Test();
    }
}

public interface ITest
{
    public void Test()
    {
        Debug.Log("InterfaceUnit");
    }
}

public class InterfaceUnit : ITest
{
    void ITest.Test()
    {
        Debug.Log("InterfaceUnit");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants