• C#后端之CodeDom在内存中创建对象最简明的讲解(中)最后附源码


    核心源码再下里,这一篇只有各个实体和反射扩展方法

    中篇就干点正事儿吧。

    先把几个实体类放上来拿去用。

    字段是这里

    using System;
    using System.CodeDom;
    
    namespace CodeMacker
    {
        public class FieldEntity
        {
            public FieldEntity(string fieldName,Type fieldType)
            {
                this.Name = fieldName;
                this.Type = fieldType;
            }
            /// <summary>
            /// 字段名称
            /// </summary>
            public string Name { get; private set; }
            /// <summary>
            /// 字段类型
            /// </summary>
            public Type Type { get; private set; }
            /// <summary>
            /// 默认初始参数
            /// </summary>
            public object DefaultValue { get; set; } = default;
            /// <summary>
            /// 字段注释
            /// </summary>
            public string Comment { get; set; } = null;
            /// <summary>
            /// 字段类型,默认公开
            /// </summary>
            public MemberAttributes Attr { get; set; } = MemberAttributes.Public | MemberAttributes.Final;
    
        }
    }
    Field Area

    属性是这里

    using System;
    using System.CodeDom;
    
    namespace CodeMacker
    {
        public class PropertyEntity 
        {
            public PropertyEntity(string propertyName, Type propertyType)
            {
                this.Name = propertyName;
                this.Type = propertyType;
            }
            /// <summary>
            /// 属性名称
            /// </summary>
            public string Name { get; private set; }
            /// <summary>
            /// 属性类型
            /// </summary>
            public Type Type { get; private set; }
            /// <summary>
            /// 注释
            /// </summary>
            public virtual string Comment { get; set; } = null;
            /// <summary>
            /// 包含Set
            /// </summary>
            public bool HasSet { get; set; } = true;
            /// <summary>
            /// 包含Get
            /// </summary>
            public bool HasGet { get; set; } = true;
            /// <summary>
            /// 属性类型,默认公开
            /// </summary>
            public MemberAttributes Attr { get; set; } = MemberAttributes.Public | MemberAttributes.Final;
        }
    }
    Property Area

    方法是这里

    namespace CodeMacker
    {
        public class MethodEntity
        {
            /// <summary>
            /// 方法体
            /// </summary>
            public string Method { get; set; }
            /// <summary>
            /// 方法注释
            /// </summary>
            public string Comment { get; set; }
        }
    }
    Method Area

    构造函数是这里

    using System;
    using System.CodeDom;
    using System.Collections.Generic;
    
    namespace CodeMacker
    {
        public class ConstructorEntity
        {
            /// <summary>
            /// 参数级
            /// </summary>
            public List<ParamInfo> Params { get; set; }
    
            /// <summary>
            /// 构造函数类型
            /// </summary>
            public MemberAttributes Attr { get; set; } = MemberAttributes.Public | MemberAttributes.Final;
        }
    
        public class ParamInfo
        {
            public ParamInfo(Type paramType, string name)
            {
                ParamType = paramType;
                Name = name;
            }
            /// <summary>
            /// 类型
            /// </summary>
            public Type ParamType { get; private set; }
            /// <summary>
            /// 名称
            /// </summary>
            public string Name { get; private set; }
            /// <summary>
            /// 赋值名称
            /// </summary>
            public string ReferenceName { get; private set; }
        }
    }
    Ctor Area

    反射是这里

    using System.Reflection;
    
    namespace CodeMacker
    {
        public static class CodeMackerReflection
        {
            public static FieldInfo GetField(this object obj, string fieldName, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
                => obj.GetType().GetField(fieldName, bindingAttr);
    
            public static FieldInfo[] GetFields(this object obj, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
                => obj.GetType().GetFields(bindingAttr);
    
    
    
            public static PropertyInfo GetPropertyInfo(this object obj, string propertyName, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
                => obj.GetType().GetProperty(propertyName, bindingAttr);
    
            public static PropertyInfo[] GetPropertysInfo(this object obj, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
                => obj.GetType().GetProperties(bindingAttr);
    
    
    
            public static MethodInfo GetMethod(this object obj, string methodName, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
                => obj.GetType().GetMethod(methodName, bindingAttr);
    
    
            public static MethodInfo[] GetMethods(this object obj, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
                => obj.GetType().GetMethods(bindingAttr);
    
        }
    }
    Reflection Area

    附一张项目截图

     可以看出除了第一个Macker其他都放上来了。

    好像也写了不少,啊哈哈哈哈哈。自己骗自己。

    重头戏就放到(下)吧

    下一定是特别长的一章,嗯,一定是。

    顺路再给自己创造个流量,虽然也不知道能干吗

    using System;
    using System.CodeDom;
    
    namespace CodeMacker
    {
        public class FieldEntity
        {
            public FieldEntity(string fieldName,Type fieldType)
            {
                this.Name = fieldName;
                this.Type = fieldType;
            }
            /// <summary>
            /// 字段名称
            /// </summary>
            public string Name { get; private set; }
            /// <summary>
            /// 字段类型
            /// </summary>
            public Type Type { get; private set; }
            /// <summary>
            /// 默认初始参数
            /// </summary>
            public object DefaultValue { get; set; } = default;
            /// <summary>
            /// 字段注释
            /// </summary>
            public string Comment { get; set; } = null;
            /// <summary>
            /// 字段类型,默认公开
            /// </summary>
            public MemberAttributes Attr { get; set; } = MemberAttributes.Public | MemberAttributes.Final;
    
        
  • 相关阅读:
    django url路由
    web 协议
    动画效果 each循环
    页面载入
    js 事件 事件委托
    jQuery 文档操作
    jQuery 标签操作 样式操作
    筛选器方法
    jQuery 介绍
    js操作属性 类操作 事件
  • 原文地址:https://www.cnblogs.com/SevenWang/p/15568027.html
Copyright © 2020-2023  润新知