zoukankan      html  css  js  c++  java
  • 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;
    }
    }
  • 相关阅读:
    队列ADT
    使用QQ截图右键菜单
    字对齐、半字对齐、字节对齐的理解
    Linux虚拟机下使用USB转串口线——配置minicom、以及screen的使用
    栈的应用实例——中缀表达式转换为后缀表达式
    使用ADS1.2的注意事项及常用技巧
    VMWare虚拟机“锁定文件失败“怎么办?
    把Linux目录挂载到开发板、设置开发板从NFS启动、取消开发板从NFS启动
    关于交叉开发环境
    JavaWeb学习----JSP脚本元素、指令元素、动作元素
  • 原文地址:https://www.cnblogs.com/oneroom/p/1653540.html
Copyright © 2011-2022 走看看