gpt4 book ai didi

c# - 限制类方法对另一个类的访问

转载 作者:行者123 更新时间:2023-11-30 14:03:26 26 4
gpt4 key购买 nike

问候。

我有两个类,“数据库”和“组”。我希望能够创建“组”的实例并从“数据库”中调用这些实例的方法,并能够公开分发“组”实例引用。但是,我不想提供对“组”中的构造函数或其他方法的公共(public)访问权限。

我最初认为我可以通过将“Group”设为“Database”的私有(private)内部类来实现此访问限制,但我发现如果它是私有(private)的,我无法公开分发对“Group”的引用。另外,我试图让“Group”成为公共(public)内部类的尝试失败了,因为如果它的方法都是私有(private)的,“Database”就无法访问它们,如果它们是公共(public)的,则可以超越“Database”进行访问。

我正在寻找解决或规避此问题的最佳实践技术。也许我在某处遗漏了必要的关键字?到目前为止,我在研究中没有发现任何东西表明 C# 允许这种控制粒度。正如我在下面的代码中提供的那样,我发现了一种解决问题的困惑方法。它的本质是这样的:在'Database'中每次调用'Group'中的方法之前,在'Database'中设置一个字段,公开可读但只能私有(private)设置,'Group'的方法都检查它们的创建实例'数据库',然后再执行其预期的操作。当读取该字段(通过“数据库”中的公共(public)方法)时,该字段会被重置,以防止对“组”进行任何进一步的方法调用,直到“数据库”再次设置该字段。

public class Database {

// Field; true if Database has just authorized a method call to a %Group.
private bool group_isNextCallAuthorized = false;

// Invoked before every method call to a %Group.
private void Group_AuthorizeNextCall() {
group_isNextCallAuthorized = true;
}

// Method, ordinarily called from %Group, that checks its creating %Database
// that the %Database itself authorized the method call on the %Group. It
// automatically resets the authorization to false to prepare for the next,
// perhaps unauthorized, method call.
public bool Group_IsNextCallAuthorized() {
bool previousValue = group_isNextCallAuthorized;
group_isNextCallAuthorized = false;
return previousValue;
}

// Constructor; creates a demo %Group.
public Database() {

// Create a %Group, first authorizing the action.
Group_AuthorizeNextCall();
Group group = Group.Create(this);

// Call some method on the group
Group_AuthorizeNextCall();
group.SomeGroupMethod();

}

}

public class Group {

// Field; refers to the %Database that created this %Group instance.
private Database db;

// Generates an instance of %Group, requiring the creating %Database as an
// argument. After checking that the %Database %db isn't null, it verifies
// that %db actually requests and authorized this %Group's creation via the
// %Group_IsNextCallAuthorized(.) method provided by %Database.
public static Group Create(Database db) {

// It will not create a dud instance of %Group; it will throw an exception
// instead.
if ((db == null) || !db.Group_IsNextCallAuthorized())
throw new System.Exception("Unauthorized access.");

return new Group(db);
}

// This constructor is referenced internally by static %Create(.) as above.
private Group(Database db) {
this.db = db;
}

// This is an arbitrary method showing that it must check that the %Database
// that created this %Group authorized this method call before it will
// perform its intended function.
public void SomeGroupMethod() {
if (!db.Group_IsNextCallAuthorized())
throw new System.Exception("Unauthorized access.");

// intended functionality...
}

}

最佳答案

一种选择是将接口(interface) IGroup 公开给代码的外部部分。这个接口(interface)只有属性上的 getter,你想要访问的任何方法等等。然后数据库将在 Group 类上运行,具有对所有属性/方法的完全访问权限,并返回 IGroup

关于c# - 限制类方法对另一个类的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4170457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com