zoukankan      html  css  js  c++  java
  • C#反射

    今天学的C#反射,虽然刚开始有点头晕,但明白了还是很简单滴.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Reflection;

    namespace Reflection
    {
    class Program
    {
    static void Main(string[] args)
    {
    Assembly assembly = Assembly.LoadFrom("Reflection.exe");//程序集
    Console.WriteLine(assembly);
    Console.WriteLine();

    Type type1 = typeof(Program);
    Console.WriteLine(type1);
    Console.WriteLine();

    Program program = new Program();
    Type type2 = program.GetType();
    Console.WriteLine(type2);
    Console.WriteLine();

    Type type3 = Type.GetType("Reflection.Program", true, true);
    Console.WriteLine(type3);
    Console.WriteLine();

    Type type4 = assembly.GetType("Reflection.Program");
    Console.WriteLine(type4);
    Console.WriteLine();

    Type[] type5 = assembly.GetTypes();
    foreach (var item in type5)
    {
    Console.WriteLine(item);
    }
    Console.WriteLine();

    Type type6 = typeof(Test);
    MethodInfo[] methodinfo=type6.GetMethods();
    foreach (var item in methodinfo )
    {
    Console.WriteLine(item);
    }
    Console.WriteLine();

    Assembly assembly1 = Assembly.Load("Reflection");
    Type type7 = assembly.GetType("Reflection.Test");
    object obj = Activator.CreateInstance(type7);
    MethodInfo methodinfo1 = type7.GetMethod("Say");
    methodinfo1.Invoke(obj, new object[] { "tc" });
    Console.WriteLine();

    Type type8 = typeof(Test);
    PropertyInfo[] propertyinfo = type8.GetProperties();
    foreach (var item in propertyinfo)
    {
    Console.WriteLine(item);
    }
    Console.WriteLine();

    Assembly assembly2 = Assembly.Load("Reflection");
    Type type9 = assembly.GetType("Reflection.Test");
    object obj1 = Activator.CreateInstance(type9);
    PropertyInfo propertyinfo1 = type9.GetProperty("Name");
    var name = propertyinfo1.GetValue(obj,null);
    Console.WriteLine();


    }
    }
    class Test
    {
    string name;
    public void Say(string name)
    {
    Console.WriteLine("hello {0}", name);
    }
    public string Name
    {
    get { return name; }
    set { this.name = value; }
    }
    }
    }

  • 相关阅读:
    nvm切换node失效
    React.memo is not a function
    Path must be a string. Received true
    如何通过node读取excel文件内的数据
    js中四舍五入的方法
    为什么不推荐使用数组的includes方法---记两次includes引起的问题
    改变input复选框样式
    如何使用伪元素扩大按钮可点击区域
    简单实用的纯css按钮效果
    ie6 ie7 ie8 ie9和FireFox Chrome中css区别总结
  • 原文地址:https://www.cnblogs.com/qtc-zyl/p/4722282.html
Copyright © 2011-2022 走看看