MethodBase.IsPublic Property

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Gets a value that indicates whether this is a public method.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

Syntax

Public ReadOnly Property IsPublic As Boolean
public bool IsPublic { get; }

Property Value

Type: System..::.Boolean
true if this method is public; otherwise, false.

Examples

The following example uses the IsPublic property to display a message that indicates whether the specified method is public.

Imports System.Reflection
Imports System.Collections.Generic

Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) 

      Dim instanceAll As BindingFlags = BindingFlags.Public Or _
         BindingFlags.NonPublic Or BindingFlags.Instance

      Dim t As Type = GetType(Example)
      Dim ctorsAndMethods As New List(Of MethodBase)( _
         t.GetMethods(instanceAll))

      For Each c As ConstructorInfo In t.GetConstructors(instanceAll)
         ctorsAndMethods.Add(c)
      Next

      outputBlock.Text &= "Public?      Method or Constructor Description" & vbLf 

      For Each m As MethodBase In ctorsAndMethods 
         outputBlock.Text &= String.Format(" {0,-6}      {1}" & vbLf, _
            m.IsPublic, m.ToString())
      Next

   End Sub

   ' Example constructors.
   '
   Private Sub New() 
   End Sub

   Protected Sub New(ByVal int As Integer)
   End Sub

   Public Sub New(ByVal name As String)
   End Sub

End Class

' This example produces the following output:
' 
'Public?    Method or Constructor Description
' True      System.String ToString()
' True      Boolean Equals(System.Object)
' True      Int32 GetHashCode()
' True      System.Type GetType()
' False     Void Finalize()
' False     System.Object MemberwiseClone()
' False     Void .ctor()
' False     Void .ctor(Int32)
' True      Void .ctor(System.String)
using System;
using System.Reflection;
using System.Collections.Generic;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      BindingFlags instanceAll = BindingFlags.Public | BindingFlags.NonPublic
         | BindingFlags.Instance;

      Type t = typeof(Example);
      List<MethodBase> ctorsAndMethods = 
          new List<MethodBase>(t.GetMethods(instanceAll));

      foreach( ConstructorInfo c in t.GetConstructors(instanceAll) )
      {
         ctorsAndMethods.Add(c);
      }

      outputBlock.Text += "Public?      Method or Constructor Description\n";

      foreach(MethodBase m in ctorsAndMethods)
      {
         outputBlock.Text += String.Format(" {0,-6}      {1}\n",
            m.IsPublic, m.ToString());
      }
   }

   // Example constructors.
   //
   private Example() {}

   protected Example(int input) {}

   public Example(string name) {}
}

/* This example produces the following output:

Public?    Method or Constructor Description
 True      System.String ToString()
 True      Boolean Equals(System.Object)
 True      Int32 GetHashCode()
 True      System.Type GetType()
 False     Void Finalize()
 False     System.Object MemberwiseClone()
 False     Void .ctor()
 False     Void .ctor(Int32)
 True      Void .ctor(System.String)
 */

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

MethodBase Class

System.Reflection Namespace

Boolean

BindingFlags