您的位置:首页 > 编程语言 > C#

C#之入门总结_文件读取_21

2017-08-14 20:00 351 查看
 文件读取:

static void Main(string[] args)

        {

           //相对路径

           string path = Path.GetFullPath("青花瓷.lrc");

            Console.WriteLine(path);

            MainClass.Task01(path);

}

 public static SortedDictionary<string,string> Task01(string path)

        {

            StreamReader sr = new StreamReader(path,Encoding.Default);

            List<string> list = new List<string>();

            SortedDictionary<string, string> dic = new SortedDictionary<string, string>();

            string str = string.Empty;

            while ((str = sr.ReadLine())!=null)

            {

                list.Add(str);

            }

            for (int i = 0; i < list.Count; i++)

            {

                string[] strArr = list[i].Split(']');

                for (int j = 0; j < strArr.Length-1; j++)

                {

                    dic.Add(strArr[j].Substring(1) ,strArr[strArr.Length-1]);

                }
            }

                return dic;

        }

dl文件解析,并执行里面的方法所有的成员(不包括构造)方法:

static void Main(string[] args)

        {

 path = Path.GetFullPath("Aniaml.dll");

            MainClass.Task01(path);

    }

public static void Task01(string path)

        {

            //加载指定路径上的程序集文件的内容。

            Assembly assem = Assembly.LoadFile(path);

            //获取此程序集中定义的类型。

            Type[] ts = assem.GetTypes();

            // 使用区分大小写的搜索,从此程序集中查找指定的类型,然后使用系统激活器创建它的实例。

            object obj = assem.CreateInstance("Aniaml.Lion");

            // 摘要:

            // 表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型。

            Type t = obj.GetType();

            //返回当前 System.Type 的所有公共方法。

            MethodInfo[] methodInfoArr = t.GetMethods();

            //搜索具有指定名称的公共方法。

            MethodInfo talk = t.GetMethod("Talk");

            //使用指定的参数调用当前实例所表示的方法或构造函数。

            talk.Invoke(obj,new object[] { });

            MethodInfo show = t.GetMethod("Show");

            show.Invoke(obj,new object[] { "aa"});

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#