CodeQL 文档

类实现 ICloneable

ID: cs/class-implements-icloneable
Kind: problem
Security severity: 
Severity: recommendation
Precision: very-high
Tags:
   - reliability
   - maintainability
Query suites:
   - csharp-security-and-quality.qls

点击查看 CodeQL 代码库中的查询

实现 ICloneable 接口的目的是表明您的类的实例可以被克隆,但是有很多充分的理由说明这样做通常是一个坏主意。

首先,ICloneable.Clone() 的语义没有得到很好的定义。文档指出它“创建一个作为当前实例副本的新对象”,但它没有指定“副本”的含义。这带来了一个问题,因为可以通过两种通用方法复制对象,即深度复制或浅表复制(深度复制克隆对象及其递归引用的所有对象,而浅表复制仅克隆对象图的顶部 - 请参阅下面的示例)。由于 ICloneable 没有指定应该执行哪种类型的复制,因此不同的实现类倾向于任意选择其中一种,这使得无法确定 Clone() 的行为 - 因此该方法毫无用处。

ICloneable 的另一个问题是,如果一个类实现了它,则它的所有子类型也必须实现它,如果实现了深度复制,则其所有成员的类型也必须实现它。这不仅使得使用 ICloneable 非常容易传播,而且非常容易出错,因为很容易忘记在子类型中重写 Clone()。因此,广泛不鼓励使用 ICloneable

建议

定义您自己的 Clone()Copy() 方法,并记录它们是执行深度复制还是浅表复制。

示例

using System;

class Bad
{
    class Thing
    {
        public int I { get; set; }
        public Thing(int i) { I = i; }
    }

    class Shallow : ICloneable
    {
        public Thing T { get; set; }
        public Shallow(Thing t) { T = t; }

        // Implements a shallow clone (compliant with the spec)
        public object Clone() { return new Shallow(T); }
    }

    class Deep : ICloneable
    {
        public Thing T { get; set; }
        public Deep(Thing t) { T = t; }

        // Implements a deep clone (also compliant with the spec)
        public object Clone() { return new Deep(new Thing(T.I)); }
    }

    static void Main(string[] args)
    {
        var s1 = new Shallow(new Thing(23));
        var s2 = (Shallow)s1.Clone();
        Console.WriteLine(s2.T.I); // 23
        s1.T.I = 9;
        Console.WriteLine(s2.T.I); // 9

        var d1 = new Deep(new Thing(23));
        var d2 = (Deep)d1.Clone();
        Console.WriteLine(d2.T.I); // 23
        d1.T.I = 9;
        Console.WriteLine(d2.T.I); // 23
    }
}

参考文献

  • MSDN,《ICloneable 接口》。

  • B Wagner,《Effective C#:50 种改进 C# 代码的具体方法》(第二版),第 32 条:避免使用 ICloneable。

  • ©2025GitHub 公司
  • 条款
  • 隐私