• c# 反射基础


    代码
    using System;
    using System.Reflection;

    class Asminfo1
    {
    public static void Main(string[] args)
    {
    Console.WriteLine(
    "\nReflection.MemberInfo");

    //Get the Type and MemberInfo.
    //Insert the fully qualified class name inside the quotation marks in the following statement.
    // Type MyType = Type.GetType("System.IO.BinaryReader");

    Type MyType 
    = Type.GetType("test");

    // MemberInfo[] Mymemberinfoarray = MyType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
    MemberInfo[] Mymemberinfoarray = MyType.GetMembers();

    //Get and display the DeclaringType method.
    Console.Write("\nThere are {0} documentable members in ", Mymemberinfoarray.Length);
    Console.Write(
    "{0}.", MyType.FullName);

    foreach (MemberInfo Mymemberinfo in Mymemberinfoarray)
    {
    Console.Write(
    "\n" + Mymemberinfo.Name);
    }


    //******************************8

    System.Text.StringBuilder sb1 
    = new System.Text.StringBuilder();

    //Type t = typeof(System.Text.StringBuilder);
    Type t = sb1.GetType();
    object createObject = Activator.CreateInstance(t, "I am a instance of StringBuilder!");

    System.Text.StringBuilder sb 
    = (System.Text.StringBuilder)createObject;
    Console.Write(sb.ToString());

    string className = t.Name;

    //获取所有方法
    System.Reflection.MethodInfo[] methods = t.GetMethods();
    Console.Write(
    "\n"+ "method");
    foreach (System.Reflection.MethodInfo method in methods)
    {

    Console.Write(
    "\n" + method.Name + System.Environment.NewLine);
    }
    //获取所有成员
    // System.Reflection.MemberInfo[] members = t.GetMembers();

    //获取所有属性
    Console.Write("\n" + "property");
    System.Reflection.PropertyInfo[] properties 
    = t.GetProperties();
    foreach (System.Reflection.PropertyInfo property in properties)
    {
    Console.Write(
    "\n" + property.Name);
    }


    Console.ReadKey();

    }


    }
    public class test

    {
    private int _x;
    public int X
    {
    get { return _x; }
    set { _x = value; }
    }
    public bool isgood;

    int getX()
    {
    return _x;
    }
    }
  • 相关阅读:
    Win10导出查看删除已安装的证书
    Tomcat配置https访问
    SpringBoot启用https
    使用OpenSSL证书操作详解
    sed命令常用用法
    Jenkins安装第一个插件和通过离线安装包进行安装
    CentOS设置主机名称
    Jenkins使用过程中遇到的问题
    Visual Studio 最新插件
    文章去格式
  • 原文地址:https://www.cnblogs.com/oneroom/p/1653540.html
Copyright © 2020-2023  润新知