代码之家  ›  专栏  ›  技术社区  ›  pauldoo

是否使字段/方法只能访问同一程序集中的派生类?[副本]

  •  1
  • pauldoo  · 技术社区  · 14 年前

    这个问题已经有了答案:

    在C中的公共类上,是否有一种方法可以使字段/方法仅可访问同一程序集中的派生类?

    据我所知 protected internal 在c中,表示与 protected internal (即派生类可以访问 类),这不是我需要的。

    3 回复  |  直到 14 年前
        1
  •  3
  •   MrDustpan    14 年前

    如果您将“仅内部”字段/方法从公共类删除到内部类,该怎么办?然后,该程序集中的所有类都可以从内部类派生:

    public abstract class MyClass
    {
        public string Name { get; set; }
    }
    
    // Only classes in this assembly can derive from this class
    internal abstract class InternalClass : MyClass
    {
        protected string Other { get; set; }
    }
    
        2
  •  1
  •   Mike Dour    14 年前

    您不能阻止来自外部程序集的类编译对它的调用,但如果通过将此属性放在受保护的成员上,则可以强制在运行时发生异常:

       [System.Security.Permissions.StrongNameIdentityPermission(
        System.Security.Permissions.SecurityAction.LinkDemand, 
        PublicKey = "...<Your assembly's full public key>...")]
    
        3
  •  1
  •   TreDubZedd    14 年前

    如果班级是 internal ,将无法在程序集之外对其进行子类划分。制作字段/方法 protected 你很高兴去。

    如果该类必须是 public ,使用 内部的 集合中的所有子类。