C#反射与进程

反射:用处为当你解析一个类的时候使用。在工作中比较常用。

代码如下,有的结果我写在了注释后面,不过具体结果大家试一试才能看到结果。

首先,引入反射的命名空间:using System.Reflection;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;//引用反射命名空间

namespace _001反射
{
    class Program
    {
        static void Main(string[] args)
        {         
            //元数据:程序或者程序集本身
            //反射:在运行运行状态下,可以查看其他程序集和本程序的元数据,这种行为就是反射
            //MyClass9 my = new MyClass9();//这个my用来存储该类的数据,但是成员存储在哪里?同type存储类的信息成员
           // Type type= my.GetType();//通过对象获取到所属对象类的type或者说是反射对象,这是第一种拿到反射对象的方式
           // Type type=  typeof(MyClass9);//获取当前类型第二种获取反射对象的方式
            Type type = Type.GetType("_001反射.MyClass9");//这是第三种拿到反射对象的方式

            //Console.WriteLine(type.Name);//拿到该类的名字
            //Console.WriteLine(type.Namespace);//拿到该类所属的命名空间
            //Console.WriteLine(type.Assembly);//拿到该类所属程序集

            //反射出该类字段的信息
            //FieldInfo[] array1= type.GetFields();//反射出该类的所有的字段
            //foreach (var item in array1)//反射只能拿到public的字段成员
            //{
            //    Console.WriteLine(item.Name);
            //}
            //object obj= Activator.CreateInstance(type);//创建一个实例对象,由于不知道该类型是什么类型,用object
            //array1[0].SetValue(obj,20);//给该类的成员进行赋值
            //array1[1].SetValue(obj,5);
            //array1[2].SetValue(obj, 500);

            //for (int i = 0; i < array1.Length; i++)//打印赋值后的成员的值
            //{
            //    Console.WriteLine(array1[i].GetValue(obj));
            //}

            //反射该类型的属性
            // PropertyInfo[] array2= type.GetProperties();//拿到该类型的所有的属性
            //foreach (var item in array2)
            //{
            //    Console.WriteLine(item.Name);
            //}

            //反射该类型的方法
            //MethodInfo[] array3= type.GetMethods();
            //foreach (var item in array3)//遍历出来的是所有的方法,包括属性里的get,set方法,以及基类的方法(object)
            //{
            //    Console.WriteLine(item.Name);
            //}

            //怎么反射对象中的静态方法
            //MethodInfo t=  type.GetMethod("Text3");//拿到静态无参的方法信息
            //t.Invoke(null,null);
            //MethodInfo t1 = type.GetMethod("Text4");//拿到有参数的方法信息
            //object[] objs = new object[] { 10 };
            //t1.Invoke(null,objs);

            //怎么拿到对象中的非静态方法内容
            //MethodInfo t = type.GetMethod("Text2");//有参数的非静态方法
            //object[] o = {"qwe" };
            //t.Invoke(obj,o);

            //通过对象获取所在程序集
            MyClass9 my = new MyClass9();
            Assembly assem=  my.GetType().Assembly;
            // Console.WriteLine(assem.FullName);
            Type[] type1= assem.GetTypes();//拿到该程序集中含有的类型有多少
            foreach (var item in type1)
            {
                Console.WriteLine(item);//每一个类型
            }

            //我们通过type类对象能反射出所有类型的成员信息(public)












        }
    }
}

进程:

引入命名空间using System.Diagnostics;

class Program
    {
        static void Main(string[] args)
        {
            Process[] progress = Process.GetProcesses();
            foreach (var item in progress)
            {
                Console.WriteLine(item.Id + "    " + item.ProcessName);
                if (item.Id == 532) {

                    item.Kill();//关闭QQ
                }
            }
            // Process.Start("calc");//打开计算器
            Process.Start("mspaint");//画图
            Process.Start("notepad");//记事本
            //Process.Start("iexplore", "http://www.baidu.com");//打开一个网址
        }
    }

后面的进程比较好玩,可以进行插入网址广告什么的。

原文地址:https://www.cnblogs.com/shuanglu/p/8287828.html