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; }
    }
    }
    }

  • 相关阅读:
    (数据科学学习手札21)sklearn.datasets常用功能详解
    (数据科学学习手札20)主成分分析原理推导&Python自编函数实现
    (数据科学学习手札19)R中基本统计分析技巧总结
    (数据科学学习手札18)二次判别分析的原理简介&Python与R实现
    P2633|主席树+dfs序+树链剖分求lca+离散化
    主席树|求区间第k小模板
    树上问题
    数据结构|序列问题与树上问题小结
    珂朵莉树 例题小结
    CF#609E|二分+树状数组
  • 原文地址:https://www.cnblogs.com/qtc-zyl/p/4722282.html
Copyright © 2011-2022 走看看