C# 自定义克隆方法是否隐藏继承的成员?

C# 自定义克隆方法是否隐藏继承的成员?,c#,C#,'TestApp.IMyInterface.Clone()'隐藏继承的成员 'System.ICloneable.Clone()'。如果需要隐藏,请使用新关键字 有意的 我需要我的界面与iClonable兼容。我如何解决这一歧义 更新 考虑这个具体的类,实现IMyInterface。这个想法是,它应该有一个工作的克隆方法和实现ICloneable,这样任何接受ICloneable的方法都可以工作 public interface IMyInterface : ICloneable { I

'TestApp.IMyInterface.Clone()'隐藏继承的成员 'System.ICloneable.Clone()'。如果需要隐藏,请使用新关键字 有意的

我需要我的界面与iClonable兼容。我如何解决这一歧义

更新 考虑这个具体的类,实现
IMyInterface
。这个想法是,它应该有一个工作的
克隆
方法实现
ICloneable
,这样任何接受
ICloneable
的方法都可以工作

public interface IMyInterface : ICloneable
{
    IMyInterface Clone();
}

现在,这将编译,但有一个警告。如何消除该警告并保持与
ICloneable
接口的兼容性?

您收到该警告是因为
ICloneable
指定了一个返回
对象的
Clone
方法;不是
IMyInterface
。您的
Clone
方法具有不同的签名,因此隐藏了
ICloneable
接口指定的签名。你可以不说了:

public class MyClass : IMyInterface
{
    #region ICloneable interface
    object ICloneable.Clone()
    {
        return this.Clone();
    }
    #endregion

    public IMyInterface Clone()
    {
        return new MyClass();
    }
}
如果两者都需要,则使用
new
关键字:

public interface IMyInterface : ICloneable
{
}
使您的实现如下所示:

public interface IMyInterface : ICloneable
{
    new IMyInterface Clone();
}
这将满足两个接口,并且不会在每个
Clone
实现中重复代码

用法:

public class MyInterface : IMyInterface
{
    object ICloneable.Clone() // explicit interface implementation
    {
        return this.Clone(); // calls the other Clone method
    }

    public IMyInterface Clone()
    {
        return new MyInterface
        {
            // member initializations
        };
    }
}

您得到该警告是因为
ICloneable
指定了一个
Clone
方法,该方法返回一个
对象
;不是
IMyInterface
。您的
Clone
方法具有不同的签名,因此隐藏了
ICloneable
接口指定的签名。你可以不说了:

public class MyClass : IMyInterface
{
    #region ICloneable interface
    object ICloneable.Clone()
    {
        return this.Clone();
    }
    #endregion

    public IMyInterface Clone()
    {
        return new MyClass();
    }
}
如果两者都需要,则使用
new
关键字:

public interface IMyInterface : ICloneable
{
}
使您的实现如下所示:

public interface IMyInterface : ICloneable
{
    new IMyInterface Clone();
}
这将满足两个接口,并且不会在每个
Clone
实现中重复代码

用法:

public class MyInterface : IMyInterface
{
    object ICloneable.Clone() // explicit interface implementation
    {
        return this.Clone(); // calls the other Clone method
    }

    public IMyInterface Clone()
    {
        return new MyInterface
        {
            // member initializations
        };
    }
}
此编译器警告告诉您,您的代码令人困惑;阅读它的人可能不确定您是试图覆盖基本成员还是创建新成员

您应该添加
new
关键字来说明您正在创建一个新成员

请注意,实现接口的类将需要同时实现两种
Clone()
方法;其中至少有一个需要显式实现。

此编译器警告告诉您,您的代码令人困惑;阅读它的人可能不确定您是试图覆盖基本成员还是创建新成员

您应该添加
new
关键字来说明您正在创建一个新成员


请注意,实现接口的类将需要同时实现两种
Clone()
方法;其中至少有一个需要显式实现。

您的方法与克隆()的IClonable接口规范同名。你应该考虑让你的iMyDe界面更明确地指定你的克隆所做的事情。示例:DeepCopy()/ShallowCopy()。 或者,您可以使用以下方法简单地替换基本克隆()

IMyInterface i = new MyInterface();
MyInterface c = new MyInterface();

object x = i.Clone(); // Calling Clone on i calls the ICloneable.Clone implementation
IMyInterface y = c.Clone(); // Calling Clone on c calls the IMyInterface.Clone implementation

您的方法与Clone()的IClonable接口规范具有相同的名称。你应该考虑让你的iMyDe界面更明确地指定你的克隆所做的事情。示例:DeepCopy()/ShallowCopy()。 或者,您可以使用以下方法简单地替换基本克隆()

IMyInterface i = new MyInterface();
MyInterface c = new MyInterface();

object x = i.Clone(); // Calling Clone on i calls the ICloneable.Clone implementation
IMyInterface y = c.Clone(); // Calling Clone on c calls the IMyInterface.Clone implementation

使用
new
关键字?或MyClone代替cloneue使用
new
关键字?或MyClone代替CloneNo,我需要这两种方法。在一个具体的类中,这是没有问题的。我只需添加
对象ICloneable.Clone()
MyConcreteClass Clone()
。我已经修改了我的答案。谢谢你的全面回答。解决方案是添加
new
关键字,正如您所示。不,我需要两种方法。在一个具体的类中,这是没有问题的。我只需添加
对象ICloneable.Clone()
MyConcreteClass Clone()
。我已经修改了我的答案。谢谢你的全面回答。解决方案是添加
new
关键字,就像您所展示的那样。这就是我所做的。在声明前添加
new
似乎就是答案。这就是我所做的。在声明前添加
new
似乎就是答案。