Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 隐藏cs文件之外的类的成员,但可对文件内的类进行访问_C# - Fatal编程技术网

C# 隐藏cs文件之外的类的成员,但可对文件内的类进行访问

C# 隐藏cs文件之外的类的成员,但可对文件内的类进行访问,c#,C#,如果我有一个包含2个类的cs文件,并且希望保护我的类的成员不受外部世界的影响,但是让这些类可以访问同一文件中的所有成员。这能做到吗 我想我不希望访问修饰符“internal”处理完整的App_代码文件夹,而是处理这个文件 简化示例: public class Comments { private static List<CommentRecord> Records = new List<CommentRecord>(); static Comments()

如果我有一个包含2个类的cs文件,并且希望保护我的类的成员不受外部世界的影响,但是让这些类可以访问同一文件中的所有成员。这能做到吗

我想我不希望访问修饰符“internal”处理完整的App_代码文件夹,而是处理这个文件

简化示例:

public class Comments {
    private static List<CommentRecord> Records = new List<CommentRecord>();
    static Comments() {
        LoadFromDatabase();
    }

    public static void LoadFromDatabase() {
        // load and loop to fill data...
        CommentRecord comment = new CommentRecord();
        comment.Id = ...
        comment.ParentId = ...
        comment.Text = ...
        Records.Add(comment);
    }

    public static CommentRecord FindById(Int32 id) {
        return Records.Find(delegate(CommentRecord x){return x.Id == id;});
    }
}

public class CommentRecord {
    public Int32 Id = 0;
    public Int32 ParentId = 0;
    public String Text = "";

    public void Save() {
        // save...
    }
}
公共类注释{
私有静态列表记录=新列表();
静态注释(){
LoadFromDatabase();
}
公共静态void LoadFromDatabase(){
//加载并循环以填充数据。。。
CommentRecord comment=新CommentRecord();
comment.Id=。。。
comment.ParentId=。。。
comment.Text=。。。
记录。添加(注释);
}
公共静态CommentRecord FindById(Int32 id){
return Records.Find(委托(CommentRecord x){return x.Id==Id;});
}
}
公开课堂评论记录{
公共Int32 Id=0;
public Int32 ParentId=0;
公共字符串Text=“”;
公共作废保存(){
//保存。。。
}
}
我不希望其他外部类接触Id字段,但是同一文件中的Comments类应该能够设置它

  • 将Id设置为属性可以使它对外部世界是只读的,但是我也不能通过Comments类更新它
  • 将Id设为私有不会授予Comments类对其的访问权限
  • 将Id设为内部将授予对Comments类的访问权限,但也会授予对App_Code文件夹中所有其他内容的访问权限

您需要的是一个公共接口,带有一个私有的内部类,因此只有Comments类可以接触CommentsRecord

public interface ICommentRecord
{
    int Id { get; }
    // Expose any other public members you want
}

public class Comments
{
    private static List<CommentRecord> Records = new List<CommentRecord>();
    static Comments()
    {
        LoadFromDatabase();
    }

    public static void LoadFromDatabase()
    {
        // load and loop to fill data...
        CommentRecord comment = new CommentRecord();
        comment.Id = 2;
        Records.Add(comment);
    }

    // Make this returnn the public interface
    public static ICommentRecord FindById(Int32 id)
    {
        return Records.Find(delegate(CommentRecord x) { return x.Id == id; });
    }

    // Make this class private to Comments class. So its only accessible from within Comments
    private class CommentRecord : ICommentRecord
    {
        public int Id { get; internal set; }  
    }

}
公共接口ICommentRecord
{
int Id{get;}
//公开您想要的任何其他公共成员
}
公开课评论
{
私有静态列表记录=新列表();
静态注释()
{
LoadFromDatabase();
}
公共静态void LoadFromDatabase()
{
//加载并循环以填充数据。。。
CommentRecord comment=新CommentRecord();
注释Id=2;
记录。添加(注释);
}
//将此返回设置为公共界面
公共静态ICommentRecord FindById(Int32 id)
{
return Records.Find(委托(CommentRecord x){return x.Id==Id;});
}
//使该类成为Comments类的私有类。因此它只能从Comments中访问
私有类注释记录:ICommentRecord
{
公共整数Id{get;内部集合;}
}
}

您的另一个选择是将类放在单独的程序集中,并使用
内部
简短回答:不,不可能

您可以通过将
Comments
类嵌套在
CommentRecord
类中来接近它,这样它就可以访问
CommentsRecord
类的
private
成员。但这不是一个好的解决方案,除非您有其他合理的理由以这种方式嵌套类。如果您不这样做,那么您应该只使用
internal
,并接受同一项目中的其他代码可能会对其进行更改


更好的做法是,IMHO将字段设置为只读(作为字段或属性),并且不授予
注释
类对属性设置器的访问权。相反,将类设计为不可变类型,并要求
Comments
类将正确的值传递给构造函数。如果没有更多的上下文,就不可能确定这是否适用于您的情况,但这肯定是一种更好的设计。

只需使用构造函数。@EhsanSajjad
internal
关键字使程序集可以访问类,而不是命名空间。这就是说,我想说,如果您真的需要一个类只允许少数其他类访问(出于某种原因),那么它们可能应该在自己的程序集中。