C Sharp中的"friend"关键字等效吗?

编程入门 行业动态 更新时间:2024-10-20 05:27:13
本文介绍了C Sharp中的"friend"关键字等效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

C Sharp中的'friend'关键字等同于什么?

What is the equivalent of a 'friend' keyword in C Sharp?

如何使用内部"关键字?

How do I use the 'internal' keyword?

我已经读到'internal'关键字可以替代C#中的'friend'.

I have read that 'internal' keyword is a replacement for 'friend' in C#.

我在C#项目中使用的DLL具有我的源代码,但是我不想修改现有的代码.我已经继承了该类,并且可以根据需要使用继承的类.问题在于父类中的大多数代码都具有受保护的方法.使用朋友会以某种方式使访问或调用这些受保护的方法成为可能吗?

I am using a DLL in my C# project that I have the source code for and yet I do not want to modify the existing code. I have inherited the class and I can use my inherited class any way I want. The problem is that most of the code in the parent class has protected methods. Will using a friend somehow make it possible to access or call these protected methods?

推荐答案

  • 您可以使用关键字访问修饰符 internal 将一个类型或类型成员声明为只能在同一程序集中的代码中访问.

  • You can use the keyword access modifier internal to declare a type or type member as accessible to code in the same assembly only.

    您可以使用 InternalsVisibleToAttribute 在 System.Rutime.CompilerServices 中定义的类将类型声明为可在同一程序集中或仅在指定程序集中的代码访问.

    You can use the InternalsVisibleToAttribute class defined in System.Rutime.CompilerServices to declare a type as accessible to code in the same assembly or a specified assembly only.

    您可以像使用其他任何访问修饰符一样使用第一个作为private.发挥作用:

    You use the first as you use any other access modifier such as private. To wit:

    internal class MyClass { ... }

    您使用第二个方法如下:

    You use the second as follows:

    [assembly:InternalsVisibleTo("MyFriendAssembly", PublicKey="...")] internal class MyVisibleClass { ... }

    这两个都可以视为C#中friend的等效项.

    Both of these can rightly be considered the equivalent of friend in C#.

    protected 的方法已可用于派生类

    Methods that are protected are already available to derived classes.

  • 更多推荐

    C Sharp中的"friend"关键字等效吗?

    本文发布于:2023-11-11 06:03:31,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1577580.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:关键字   Sharp   quot   friend

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!