简体   繁体   English

访问修饰符,继承和外部汇编

[英]Access Modifier, Inheritance and Foreign Assembly

let's say I have a baseClass in myFramework namespace: 假设我在myFramework命名空间中有一个baseClass:

using System;
using System.Collections.Generic;
using System.Text;

namespace myFramework
{
    public class baseClass
    {
        internal int m_value;

        internal int getValue() {
            return m_value;
        }

        internal void setValue(int value) {
            m_value = value;
        }

    }
}

In MyApplication namespace I inherit from it: 在MyApplication命名空间中,我继承自它:

using System;
using System.Collections.Generic;
using System.Text;
using myFramework;

namespace MyApplication
{
    class InheritedClass: baseClass
    {
    }
}

In a form in MyApplication namespace I use it: 在MyApplication命名空间的表单中,使用它:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using myFramework;

namespace MyApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InheritedClass inheritedClass = new InheritedClass();

            inheritedClass.setValue(10);
            textBox1.Text = inheritedClass.m_value.ToString();
        }
    }
}

As you can see I have declared m_value with internal modifier so that I can access it from Form. 如您所见,我已经使用内部修饰符声明了m_value,以便可以从Form中访问它。

Now what if I compile myFramework in a separate assembly ? 现在,如果我在单独的程序集中编译myFramework怎么办? internal won't work any more ? 内部不再工作了吗? So is there any modifier I can use ? 那么我可以使用任何修饰符吗? Protected can't even do less the job. 受保护甚至无法减少工作量。 Am I obliged to use public ? 我有义务使用公开吗?

Isn't there an access modifier that allows an object (form) which OWNS a class (inheritedclass) to access all the members of all class and parent class he owns like internal except that internal doesn't consider ownership but where the class has been declared ? 是否没有访问修饰符,该修饰符允许拥有OWNS类(继承的类)的对象(窗体)访问他拥有的所有类和父类的所有成员,就像Internal一样,除了内部不考虑所有权而是该类所在的位置宣布?

Update: of course in real world use, my base class has tight semantics with form (or view) so it's up to me to decide if I should do it, my question is not if I should do, my question is if it is possible or if I will be obliged to use stuff like Design by Contracts which I find silly to add yet another tool for such basic requirements. 更新:当然,在现实世界中使用时,我的基类在形式(或视图)方面具有严格的语义,因此要由我决定是否应该这样做,我的问题不是是否应该这样做,我的问题是是否可能或者如果我不得不使用“按合同设计”之类的东西,而我发现自己愚蠢地为这种基本要求添加了另一种工具。

The form does not "own" the class, the form can be said to "own" an instance of that class, but even that is rather unclear; 表单并不“拥有”该类,可以说该表单“拥有”了该类的一个实例,但是即使这样还不清楚。 normally we use "own" to mean "has ultimate responsibility for", in the case where an object has to be disposed or otherwise "cleaned-up" at some point. 通常,在必须处置某个物体或以其他方式“清理”某个物体的情况下,我们使用“拥有”来表示“对此负有最终责任”。

The form has absolutely nothing to do with the inner workings of baseClass. 该形式与baseClass的内部工作完全无关。 If form needs to access a member of baseClass, it should be public. 如果表单需要访问baseClass的成员,则它应该是公共的。 Alternatively, if a member of baseClass should be private, internal, protected or protected internal, then the form has no business dealing with it. 另外,如果baseClass的成员应该是私有的,内部的,受保护的或受保护的内部成员,则该表单没有业务处理。

If you are looking to give an unrelated class access to a deliberately non-public member, something has gone wrong in your design. 如果您希望给不相关的类访问故意的非公共成员的权限,则说明您的设计出现了问题。

"Ownership" has no meaning here, these are completely unrelated classes. “所有权”在这里没有意义,它们是完全无关的类。 At best you could jump through the InternalsVisibleTo attribute hoop. 充其量您可以跳过InternalsVisibleTo属性框。

Based on the example shown, a public property would do exactly what you need. 根据显示的示例,公共财产将完全满足您的需求。 However, if you want to keep the member hidden (and you know ahead of time exactly which assemblies will need access to it), the InternalsVisibleToAttribute will do the trick. 但是,如果要隐藏成员(并且您提前知道要访问哪些程序集),则InternalsVisibleToAttribute可以解决问题。

I use this in instances where I do not want a property to be exposed, but I need access to it for testing. 我在不希望公开属性但需要访问它以进行测试的情况下使用它。 In this case, I know precisely that I only want my test assembly to have access to the property and I have control over how it is used. 在这种情况下,我确切地知道我只希望测试程序集可以访问该属性,并且可以控制其使用方式。

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

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