反射_读取和删除文件<15/9/2017>
在桌面创建文本文档,写入执行的类,方法和路径
写入代码实现根据文本文档内容创建文件或删除的功能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
namespace 作业
{
class Program
{
static void Main(string[] args)
{
try
{
string[] line = File.ReadAllLines(@"C:\Users\Maximilian Liu\Desktop\list.txt", Encoding.GetEncoding("gbk"));
Type t = Type.GetType(line[0]);
object o = t.GetConstructors()[0].Invoke(null);
MethodInfo mm = t.GetMethod(line[1], new Type[] { typeof(string) });
mm.Invoke(o, new object[] { line[2] });
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
public class FileOperator
{
public void Create(string path)
{
File.Create(@path);
}
public void Delete(string path)
{
File.Delete(@path);
}
}
}