• dot net 杂谈之一



    一、使用vscode开发.net core程序

    安装如下插件:
    1、vscode-solution-explorer
    在这里插入图片描述
    2、.NET Core Extension Pack
    在这里插入图片描述
    3、搜索nuget工具包并安装

    二、创建解决方案

    图形化创建
    在这里插入图片描述
    通过命令行创建

    dotnet new sln -n "study001"
    
    • 1

    创建asp.net core webapi
    在这里插入图片描述
    创建webapi的时候.net 5和.net 6创建出来的程式代码写法是不一样的,我们安装.net 6 sdk
    .net 6 sdk下载

    三、反射

    3.1 反射应用场景一

    using System.Data;
    
    // 1: 构建虚拟数据
    var data = new DataTable();
    data.Columns.AddRange(new DataColumn[3]
    {
        new DataColumn("Id"),
        new DataColumn("Name"),
        new DataColumn("NickName")
    });
    data.Rows.Add(1, "张三", "小张子");
    data.Rows.Add(1, "李四", "小李子");
    data.Rows.Add(1, "王五", "小五子");
    
    // 2:给DataTable加一个扩展方法,直接从中获取到List
    List<Student> students = data.GetEntityList<Student>();
    
    // 3:我们定义的对象属性不一定和数据库里的字段一致,加一些特性
    
    foreach(Student student in students)
    {
        Console.WriteLine($"{student.Id}--{student.Name}--{student.NickName}");
    }
    
    
    
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string NickName { get; set; }
    }
    
    // 扩展方法必须定在静态类中
    public static class DataTableExtensions
    {
        public static List<TEntity> GetEntityList<TEntity>(this DataTable data) where TEntity:class, new()
        {
            // 用来装转换后的数据
            var list = new List<TEntity>();
            // 拿到TEntity的所有属性
            var properties = typeof(TEntity).GetProperties();
            foreach(DataRow row in data.Rows)
            {
                var entity = new TEntity();
                // 为这个entity赋值
                foreach(var prop in properties)
                {
                    // 数据中的所有字段
                    foreach(DataColumn column in data.Columns)
                    {
                        if(prop.Name.Equals(column.ColumnName, StringComparison.OrdinalIgnoreCase))
                        {
                            // 将实体中取出来的值的类型转换成对象TEntity的属性类型
                            var value = Convert.ChangeType(row[column], prop.PropertyType);
                            prop.SetValue(entity, value);
                        }
                    }
                }
                list.Add(entity);
            }
            return list;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    带特性Attribute的反射

    using System.Data;
    using System.Reflection;
    
    // 1: 构建虚拟数据
    var data = new DataTable();
    data.Columns.AddRange(new DataColumn[3]
    {
        new DataColumn("t_id"),
        new DataColumn("t_name"),
        new DataColumn("t_nickname")
    });
    data.Rows.Add(1, "张三", "小张子");
    data.Rows.Add(1, "李四", "小李子");
    data.Rows.Add(1, "王五", "小五子");
    
    // 2:给DataTable加一个扩展方法,直接从中获取到List
    List<Student> students = data.GetEntityList<Student>();
    
    // 3:我们定义的对象属性不一定和数据库里的字段一致,加一些特性
    
    foreach(Student student in students)
    {
        Console.WriteLine($"{student.Id}--{student.Name}--{student.NickName}");
    }
    
    // 定义特性,其实也是一个类
    public class FieldNameAttribute : Attribute
    {
        public FieldNameAttribute(string fieldName)
        {
            FieldName = fieldName;
        }
    
        public string FieldName { get; }
    }
    
    
    
    public class Student
    {
        [FieldName("t_id")]
        public int Id { get; set; }
        [FieldName("t_name")]
        public string Name { get; set; }
        [FieldName("t_nickname")]
        public string NickName { get; set; }
    }
    
    // 扩展方法必须定在静态类中
    public static class DataTableExtensions
    {
        public static List<TEntity> GetEntityList<TEntity>(this DataTable data) where TEntity:class, new()
        {
            // 用来装转换后的数据
            var list = new List<TEntity>();
            // 拿到TEntity的所有属性
            var properties = typeof(TEntity).GetProperties();
            foreach(DataRow row in data.Rows)
            {
                var entity = new TEntity();
                // 为这个entity赋值
                foreach(var prop in properties)
                {
                    // 数据中的所有字段
                    foreach(DataColumn column in data.Columns)
                    {
                        // 是否打了特性指定了ColumnName
                        if(prop.IsDefined(typeof(FieldNameAttribute), false))
                        {
                            var fieldNameAttribute = prop.GetCustomAttribute<FieldNameAttribute>();
                            if(fieldNameAttribute.FieldName.Equals(column.ColumnName, StringComparison.OrdinalIgnoreCase))
                            {
                                var value = Convert.ChangeType(row[column], prop.PropertyType);
                                prop.SetValue(entity, value);
                            }
                        }
                        else if(prop.Name.Equals(column.ColumnName, StringComparison.OrdinalIgnoreCase))
                        {
                            // 将实体中取出来的值的类型转换成对象TEntity的属性类型
                            var value = Convert.ChangeType(row[column], prop.PropertyType);
                            prop.SetValue(entity, value);
                        }
                    }
                }
                list.Add(entity);
            }
            return list;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89

    3.2 反射应用场景二

    有一些ORM你将对象传进一个函数,它就给你处理插入到数据库中,这是怎么弄的呢?
    如:
    var student = new Student{};
    conn.Insert(student); //ORM怎么处理的?

  • 相关阅读:
    正则提取字符串中的年龄对大于50的求和
    LeetCode 刷题系列 -- 2. 两数相加
    网络安全项目简介
    【带头学C++】----- 1.基础知识 ---- 1.24 逻辑控制语句
    继承(1)
    php+vue+Elementui大学生心理健康测评网站
    【面经】米哈游大数据开发一面二面面经
    Docker 保存与发布(commit, save, load)
    腾讯内部疯传MyBatis实践指南,让味同嚼蜡的知识生动有趣
    假设检验3
  • 原文地址:https://blog.csdn.net/weixin_42710036/article/details/127799253