英語で読む

次の方法で共有


MethodBase.IsPublic プロパティ

定義

パブリック メソッドかどうかを示す値を取得します。

C#
public bool IsPublic { get; }

プロパティ値

このメソッドがパブリックの場合は true。それ以外の場合は false

実装

次の例では、 プロパティを IsPublic 使用して、指定したメソッドがパブリックかどうかを示すメッセージを表示します。

C#
class methodbase
{
   public static int Main(string[] args)
   {

      Console.WriteLine("\nReflection.MethodBase");

      //Get the MethodBase of a method.

      //Get the type
      Type MyType = Type.GetType("System.MulticastDelegate");

      //Get and display the method
      MethodBase Mymethodbase =
         MyType.GetMethod("RemoveImpl",BindingFlags.NonPublic);

      Console.Write("\nMymethodbase = " + Mymethodbase);

      bool Myispublic = Mymethodbase.IsPublic;
      if (Myispublic)
         Console.Write ("\nMymethodbase is a public method");
      else
         Console.Write ("\nMymethodbase is not a public method");

      return 0;
   }
}
/*
Produces the following output

Reflection.MethodBase
Mymethodbase = System.Delegate RemoveImpl (System.Delegate)
Mymethodbase is not a public method
*/

注釈

を取得するには、最初に MethodBase型を取得します。 型から メソッドを取得します。 メソッドから を取得します MethodBaseMethodBaseまたは コンストラクターがパブリック以外の場合は保護され、簡単にアクセスすることはできません。 非パブリック メソッドにアクセスするには、 で GetMethodマスクを にNonPublic設定しますBindingFlags

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください