# Study notes .net base reinforcement 2020.2.13

1.1.String.Split () function

Split string

   temp[0]张三  temp[1]5000
      string[] temp = contents[i].Split(new char[] { ' ' }, StringSplitOptions.Remove

2.Directory class (folder operations)

         //CreateDirectory:创建一个新的文件夹
            //Delete:删除
            //Move:剪切
            //Exist()判断指定的文件夹是否存在
       if (!Directory.Exists(@"C:\Users\SpringRain\Desktop\new"))
           {
               Directory.CreateDirectory(@"C:\Users\SpringRain\Desktop\new");
         }  
         
         //创建100个
            for (int i = 0; i < 100; i++)
            {
                Directory.CreateDirectory(@"C:\Users\SpringRain\Desktop\new\" + i);
         }            

Move and delete

 Directory.Move(@"C:\Users\SpringRain\Desktop\new", @"C:\Users\SpringRain\Desktop\new2");
        Console.WriteLine("移动成功");
        Directory.Delete(@"C:\Users\SpringRain\Desktop\new2", true);
        Console.WriteLine("删除成功");
        Console.ReadKey();

3. commissioned

Client: If we want to pass a method as a parameter, it is necessary to use a delegate. In simple terms delegate
is a type, which can be assigned a reference method.
For delegate type can be assigned a reference to a method, a method can assign up, call this method through the delegate variable
method of packaging delegate must meet the following rules:
1: signature method must be consistent with the commission, including the number of parameters in the method signature , cis-type
sequence.
2: the return type to be commissioned and consistent.
(1) anonymous function and lamda expressions wording

public delegate void Del1();//申明委托
 static void Main(string[] args)
        {
            Del1 del1 = Test1;
            Del1 del1 = delegate() { }//匿名函数
            Del1 del1 = () => { };//lamda表达式
}
static void Test1()
        {
            .....
        }

(2) transfer values between commissioned form
Form2 function uses the Form1
Form1

  private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(ShowText);//showText作为参数函数
            frm2.Show();
        }
 //将Form2传送过来的数据 赋值给Label
        void ShowText(string str)
        {
            label1.Text = str;
        }

Form2

 public delegate  void DelTest(string str);//申明委托
  public partial class Form2 : Form
    {
        private DelTest _del;//存储Form1传送过来的函数
        public Form2(DelTest del)  //参数
        {
            this._del = del;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this._del(textBox1.Text.Trim()); 
            
        }

4. Assembly

.net dll and exe are in the assembly.
Assembly (Assembly), can be seen as a bunch of related classes make a package.
Assembly contains data files, type metadata.
The benefits of using the assembly: the assembly only references program necessary to reduce the size of the program; package assemblies contract some code to provide only the necessary access interface.
Copy the .dll to the directory

    using System.Reflection;
     using System.IO; //命名空间
  string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Common.dll");//绝对路径
            Assembly ass = Assembly.LoadFile(path)
            Console.WriteLine("加载成功");
             //获取程序集的所有公开的数据
            Type[] types1=ass.GetExportedTypes();
            //获取程序集的数据,包含公开和不公开
           Type[] types2 = ass.GetTypes();
           foreach (Type item in types1)
           {
               Console.WriteLine(item.Name);
           }

         Type t = ass.GetType("Common.Person");
            Console.WriteLine(t.Name);

            Console.ReadKey();
            
 获取程序集.dll 里的数据
 

```csharp
using System.Reflection;  //命名空间
  Assembly ass=Assembly.LoadFile("绝对路径.dll")//加载
 


 //调用Person类中默认无参数的构造函数
  object o=ass.CreateInstance("Common.person");
  Console.WriteLine(o.GetType());
  
    //获取要创建的数据类型
           Type t=  ass.GetType("Common.Person");
           object o= Activator.CreateInstance(t, "十三", 12);

            //获取数据源的属性数组
           PropertyInfo[] pros = o.GetType().GetProperties();
           foreach (PropertyInfo item in pros)
           {
               Console.WriteLine(item.Name);
           }
               //获取数据源中的方法
           MemberInfo[] md = o.GetType().GetMethods();
           foreach (MemberInfo item in md)
           {
               Console.WriteLine(item.Name);
           }
            Console.ReadKey();
        }

Raw chicken dish a university, studying .net, the god of hope a lot of guidance.

Published 16 original articles · won praise 2 · Views 205

Guess you like

Origin blog.csdn.net/weixin_43482965/article/details/104291543