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;
    }
    }
  • 相关阅读:
    C# WinForm dataGridView 技巧小结
    Win7设置局域网共享
    vs2010快捷键大全
    C# WebBrowser.DocumentCompleted 多次调用解决方法
    为应用程序池 'DefaultAppPool' 提供服务的进程关闭时间超过了限制
    VB高清图标制作方法
    sqlite 中文排序
    一个vbs文件将指定文件夹下的文件名输出到指定文件夹下
    用DOS命令获取文件列表
    文件搜索神器 Everything
  • 原文地址:https://www.cnblogs.com/oneroom/p/1653540.html
Copyright © 2011-2022 走看看