📌  相关文章
📜  用于检查指定类型的 C# 程序是否公开

📅  最后修改于: 2022-05-13 01:54:51.110000             🧑  作者: Mango

用于检查指定类型的 C# 程序是否公开

类是方法、变量和对象的集合。我们可以使用访问修饰符创建公共类、私有类或受保护类。使用 public 修饰符创建的类将可以完全访问程序。因此,要检查给定的类或类型是否为公共类型,我们使用 Type 类的 IsPublic 属性。如果给定类型是公共类型,它将返回 true。否则,它将返回 false。此外,此属性不适用于嵌套类型。

句法:

public bool IsPublic { get; }

示例 1:

C#
// C# program to check whether the given class or
// type is a public type or not
using System;
using System.Reflection;
  
// Declare a class with public modifier
public class Myclass1
{
    public void display()
    {
        Console.WriteLine("Hello! GeeksforGeeks");
    }
}
  
// Declare a class without public modifier
class Myclass2
{
    public void Show()
    {
        Console.WriteLine("Hey! GeeksforGeeks");
    }
}
  
public class GFG{
  
// Driver code    
public static void Main(string[] args)
{
      
    // Check the given type is a public or not
    // Using IsPublic property
    Console.WriteLine(typeof(Myclass1).IsPublic);
    Console.WriteLine(typeof(Myclass2).IsPublic);
}
}


C#
// C# program to check whether the given class or
// type is a public type or not
using System;
using System.Reflection;
  
// Declare a class with public modifier
public class Student
{
    public void display()
    {
        Console.WriteLine("I like C# languagea alot");
    }
}
  
public class GFG{
  
// Driver code    
public static void Main(string[] args)
{
      
    // Check the given type is a public or not
    // Using IsPublic property
    if (typeof(Student).IsPublic == true)
    {
        Console.WriteLine("The given class is a public class");
    }
    else
    {
        Console.WriteLine("The given class is not a public class");
    }
}
}


输出:

True
False

示例 2:

C#

// C# program to check whether the given class or
// type is a public type or not
using System;
using System.Reflection;
  
// Declare a class with public modifier
public class Student
{
    public void display()
    {
        Console.WriteLine("I like C# languagea alot");
    }
}
  
public class GFG{
  
// Driver code    
public static void Main(string[] args)
{
      
    // Check the given type is a public or not
    // Using IsPublic property
    if (typeof(Student).IsPublic == true)
    {
        Console.WriteLine("The given class is a public class");
    }
    else
    {
        Console.WriteLine("The given class is not a public class");
    }
}
}

输出:

The given class is a public class