本文介绍了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?

推荐答案

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

  1. 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"关键字等效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:53