核心源码再下里,这一篇只有各个实体和反射扩展方法
中篇就干点正事儿吧。
先把几个实体类放上来拿去用。
字段是这里
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; } }
属性是这里
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; } }
方法是这里
namespace CodeMacker { public class MethodEntity { /// <summary> /// 方法体 /// </summary> public string Method { get; set; } /// <summary> /// 方法注释 /// </summary> public string Comment { get; set; } } }
构造函数是这里
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; } } }
反射是这里
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); } }
附一张项目截图
可以看出除了第一个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;