简体   繁体   English

公开一个可以在内部修改的只读属性

[英]Exposing a readonly property that can be modified internally

I'm creating a class that will house election results. 我正在创建一个将容纳选举结果的课程。 I have a results class that has a static method that will parse a file and return a results class with the results from the file. 我有一个具有静态方法的结果类,该方法将解析文件并返回结果类以及文件中的结果。

I want to make sure that only the static method can modify the results, so i've been using the internal modifier (Precinct.InternalCandidates) (The prevents instances of my class outside of the dll from accessing the methods, right?). 我想确保只有静态方法才能修改结果,所以我一直在使用内部修饰符(Precinct.InternalCandidates)(这可以防止dll之外的类的实例访问方法,对吗?)。

Anyway, I need to expose the candidates as a read only list to the instantiated version of my class, but I'll obviously need to be able to add candidates during the population process. 无论如何,我需要将候选人作为只读列表公开给我的课程的实例化版本,但是显然,我需要能够在填充过程中添加候选人。 So, I've created another parameter in the Precinct Class called Precinct.Candidates that exposes a read only version of InternalCandidates 因此,我在Precinct类中创建了另一个参数Precinct.Candidates,该参数公开了InternalCandidates的只读版本。

Here's how I'd envision it to work: 这是我设想的工作方式:

Results r = Results.ParseResultsFile("PathToFile.txt");
r.Candidates.Add(new Candidate) // Should error here
Console.WriteLine(r.Candidates[0].Name) // Should work

Here's what I have for my class stubs: 这是我的班级存根的内容:

public class Results {
  private List<Precinct> precincts = new List<Precinct>();
   public ReadOnlyCollection<Precinct> Precincts {
      get { return this.precincts.AsReadOnly(); }
  }

  public Results() {}

  public static Results ParseResultsFile(string filePath) { ... }
}

public class Precinct {
  internal List<Contest> InternalContests { get; set; }
  public ReadOnlyCollection<Contest> Contests {
    get { return this.InternalContests.AsReadOnly(); }
  }
  public Precinct {
    this.InternalContests = new List<Contest>();
  }
}

Is there a better way to accomplish this? 有没有更好的方法可以做到这一点?

I'm afraid I have a little bit of bad news Rob... using Reflection, one can completely circumvent access modifiers. 恐怕我还有一点坏消息Rob ...使用Reflection,可以完全规避访问修饰符。 They help to protect a team from themselves, but are not suited to providing security. 它们有助于保护团队免受自身侵害,但不适合提供安全性。

You will need to ensure the physical security of the code and ensure that nobody can load your DLL into an app domain of their own creation. 您将需要确保代码的物理安全性,并确保没有人可以将DLL加载到他们自己创建的应用程序域中。

UPDATE: 更新:

I stand corrected by myself. 我自己纠正。 You can set an attribute that prevents reflection UNLESS THE CALLER HAS FULL TRUST (update from Leppie). 您可以设置一个防止反射的属性,除非呼叫者具有完全信任(从Leppie更新)。 See how . 看看如何

You can prevent callers without full trust from accessing your private/internal methods and fields but a full trust caller cannot be prevented from using reflection. 您可以阻止没有完全信任的调用者访问您的私有/内部方法和字段,但是不能阻止完全信任的调用者使用反射。

Again. 再次。 Cleaning up my old questions... I ended up just rolling my own Collection. 清理我的旧问题...我最终只是滚动了自己的收藏集。

Worked out wonderfully.. 锻炼得很棒..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM