内部课程中的公共和内部成员?

时间:2022-09-02 09:21:17

Ok, so this may be a bit of a silly question, and there's certainly the obvious answer, but I was curious if I've missed any subtleties here.

好的,所以这可能是一个有点愚蠢的问题,肯定有明显的答案,但我很好奇,如果我错过了任何细微之处。

Is there any difference in terms of visibility/usability between a public member declared in an internal class and an internal member declared in an internal class?

在内部类中声明的公共成员与在内部类中声明的内部成员之间的可见性/可用性方面是否存在差异?

i.e. between

即之间

internal class Foo
{
    public void Bar()
    {
    }
}

and

internal class Foo
{
    internal void Bar()
    {
    }
}

If you declared the method as public and also virtual, and then overrode it in a derived class that is public, the reason for using this modifier is clear. However, is this the only situation... am I missing something else?

如果您将方法声明为public和virtual,然后在公共的派生类中将其覆盖,则使用此修饰符的原因很明显。然而,这是唯一的情况......我错过了别的什么吗?

5 个解决方案

#1


47  

Consider this case:

考虑这种情况:

public interface IBar { void Bar(); }
internal class C : IBar
{
    public void Bar() { }
}

Here C.Bar cannot be marked as internal; doing so is an error because C.Bar can be accessed by a caller of D.GetBar():

这里C.Bar不能标记为内部;这样做是一个错误,因为D.BetBar()的调用者可以访问C.Bar:

public class D
{
    public static IBar GetBar() { return new C(); } 
}

#2


30  

A public member is still just internal when in an internal class.

公共成员在内部课堂时仍然只是内部成员。

From MSDN:

来自MSDN:

The accessibility of a member can never be greater than the accessibility of its containing type. For example, a public method declared in an internal type has only internal accessibility

成员的可访问性永远不会大于其包含类型的可访问性。例如,在内部类型中声明的公共方法只具有内部可访问性

Think of it this way, I would access a public property on....? A class I can't see? :)

想一想,我会在....*问一个公共财产?我看不到的课? :)

Eric's answer is very important in this case, if it's exposed via an interface and not directly it does make a difference, just depends if you're in that situation with the member you're dealing with.

在这种情况下,Eric的答案非常重要,如果它通过界面暴露而不是直接暴露它确实有所作为,那取决于你是否与你正在处理的成员处于这种情况。

#3


1  

If it comes to reflection it matters if the member is public or not:

如果涉及到反思,那么会员是否公开就很重要:

For example you even could pass a nested private class to a WPF binding and the binding would work against the public properties just as usual.

例如,您甚至可以将嵌套的私有类传递给WPF绑定,并且绑定将像往常一样对公共属性起作用。

#4


1  

Just faced with another example where there is difference between those two, when used from XAML in WPF.

刚刚面对另一个例子,当在WPF中使用XAML时,这两者之间存在差异。

XAML:

XAML:

<Button Tag="{x:Static vm:Foo+Bar.e1}" />

Code with internal enum compiles successfully:

内部枚举的代码成功编译:

internal class Foo
{
    internal enum Bar
    {
        e1,
        e2,
    }
}

But surprisingly changing it to public results in error:

但令人惊讶的是,将其更改为公开会导致错误:

internal class Foo
{
    public enum Bar
    {
        e1,
        e2,
    }
}

The last example produces compilation error:

最后一个示例产生编译错误:

error MC3064: Only public or internal classes can be used within markup. 'Bar' type is not public or internal.

错误MC3064:在标记中只能使用公共或内部类。 “酒吧”类型不是公开的或内部的。

Unfortunately, I can't explain what's wrong with public in this case. My guess is "just because WPF works that way". Just change modifier of the nested class to internal to get rid of error.

不幸的是,在这种情况下,我无法解释公众的错误。我的猜测是“只是因为WPF以这种方式工作”。只需将嵌套类的修饰符更改为内部即可消除错误。

#5


0  

public members of an internal class can override public members of public base classes and, therefore, be a little more exposed... if indirectly.

内部类的公共成员可以覆盖公共基类的公共成员,因此,如果间接地更加暴露...

#1


47  

Consider this case:

考虑这种情况:

public interface IBar { void Bar(); }
internal class C : IBar
{
    public void Bar() { }
}

Here C.Bar cannot be marked as internal; doing so is an error because C.Bar can be accessed by a caller of D.GetBar():

这里C.Bar不能标记为内部;这样做是一个错误,因为D.BetBar()的调用者可以访问C.Bar:

public class D
{
    public static IBar GetBar() { return new C(); } 
}

#2


30  

A public member is still just internal when in an internal class.

公共成员在内部课堂时仍然只是内部成员。

From MSDN:

来自MSDN:

The accessibility of a member can never be greater than the accessibility of its containing type. For example, a public method declared in an internal type has only internal accessibility

成员的可访问性永远不会大于其包含类型的可访问性。例如,在内部类型中声明的公共方法只具有内部可访问性

Think of it this way, I would access a public property on....? A class I can't see? :)

想一想,我会在....*问一个公共财产?我看不到的课? :)

Eric's answer is very important in this case, if it's exposed via an interface and not directly it does make a difference, just depends if you're in that situation with the member you're dealing with.

在这种情况下,Eric的答案非常重要,如果它通过界面暴露而不是直接暴露它确实有所作为,那取决于你是否与你正在处理的成员处于这种情况。

#3


1  

If it comes to reflection it matters if the member is public or not:

如果涉及到反思,那么会员是否公开就很重要:

For example you even could pass a nested private class to a WPF binding and the binding would work against the public properties just as usual.

例如,您甚至可以将嵌套的私有类传递给WPF绑定,并且绑定将像往常一样对公共属性起作用。

#4


1  

Just faced with another example where there is difference between those two, when used from XAML in WPF.

刚刚面对另一个例子,当在WPF中使用XAML时,这两者之间存在差异。

XAML:

XAML:

<Button Tag="{x:Static vm:Foo+Bar.e1}" />

Code with internal enum compiles successfully:

内部枚举的代码成功编译:

internal class Foo
{
    internal enum Bar
    {
        e1,
        e2,
    }
}

But surprisingly changing it to public results in error:

但令人惊讶的是,将其更改为公开会导致错误:

internal class Foo
{
    public enum Bar
    {
        e1,
        e2,
    }
}

The last example produces compilation error:

最后一个示例产生编译错误:

error MC3064: Only public or internal classes can be used within markup. 'Bar' type is not public or internal.

错误MC3064:在标记中只能使用公共或内部类。 “酒吧”类型不是公开的或内部的。

Unfortunately, I can't explain what's wrong with public in this case. My guess is "just because WPF works that way". Just change modifier of the nested class to internal to get rid of error.

不幸的是,在这种情况下,我无法解释公众的错误。我的猜测是“只是因为WPF以这种方式工作”。只需将嵌套类的修饰符更改为内部即可消除错误。

#5


0  

public members of an internal class can override public members of public base classes and, therefore, be a little more exposed... if indirectly.

内部类的公共成员可以覆盖公共基类的公共成员,因此,如果间接地更加暴露...