zoukankan      html  css  js  c++  java
  • 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;
    
        
  • 相关阅读:
    2013-10-31 《问题儿童居然一天两更!?》
    2013-10-31 《October 31st, 2013》
    2013-10-31 《三天里什么都没干……总之把目前为止的代码发了吧……》
    日怎么没人告诉我这博客可以改博文界面的显示宽度的
    俗话说打脸哦不打铁要趁热所以记录下替换图片的方法
    GUI好看码难写不是难写是难看我是说码难看不是GUI
    虽然保持了连续代码生产量但是仔细想想也没什么必要
    重写了电话本代码全面更新居然连续三天每天一个程序
    专注写字典三十年问你怕未又被编码卡了简直难以置信
    我就写个字典居然卡了两天重申一遍文字编码日你大爷
  • 原文地址:https://www.cnblogs.com/SevenWang/p/15568027.html
Copyright © 2011-2022 走看看