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

中篇就干点正事儿吧。
先把几个实体类放上来拿去用。

字段是这里

  1. using System;
  2. using System.CodeDom;
  3. namespace CodeMacker
  4. {
  5. public class FieldEntity
  6. {
  7. public FieldEntity(string fieldName,Type fieldType)
  8. {
  9. this.Name = fieldName;
  10. this.Type = fieldType;
  11. }
  12. /// <summary>
  13. /// 字段名称
  14. /// </summary>
  15. public string Name { get; private set; }
  16. /// <summary>
  17. /// 字段类型
  18. /// </summary>
  19. public Type Type { get; private set; }
  20. /// <summary>
  21. /// 默认初始参数
  22. /// </summary>
  23. public object DefaultValue { get; set; } = default;
  24. /// <summary>
  25. /// 字段注释
  26. /// </summary>
  27. public string Comment { get; set; } = null;
  28. /// <summary>
  29. /// 字段类型,默认公开
  30. /// </summary>
  31. public MemberAttributes Attr { get; set; } = MemberAttributes.Public | MemberAttributes.Final;
  32. }
  33. }
  34. Field Area

属性是这里

  1. using System;
  2. using System.CodeDom;
  3. namespace CodeMacker
  4. {
  5. public class PropertyEntity
  6. {
  7. public PropertyEntity(string propertyName, Type propertyType)
  8. {
  9. this.Name = propertyName;
  10. this.Type = propertyType;
  11. }
  12. /// <summary>
  13. /// 属性名称
  14. /// </summary>
  15. public string Name { get; private set; }
  16. /// <summary>
  17. /// 属性类型
  18. /// </summary>
  19. public Type Type { get; private set; }
  20. /// <summary>
  21. /// 注释
  22. /// </summary>
  23. public virtual string Comment { get; set; } = null;
  24. /// <summary>
  25. /// 包含Set
  26. /// </summary>
  27. public bool HasSet { get; set; } = true;
  28. /// <summary>
  29. /// 包含Get
  30. /// </summary>
  31. public bool HasGet { get; set; } = true;
  32. /// <summary>
  33. /// 属性类型,默认公开
  34. /// </summary>
  35. public MemberAttributes Attr { get; set; } = MemberAttributes.Public | MemberAttributes.Final;
  36. }
  37. }
  38. Property Area

方法是这里

  1. namespace CodeMacker
  2. {
  3. public class MethodEntity
  4. {
  5. /// <summary>
  6. /// 方法体
  7. /// </summary>
  8. public string Method { get; set; }
  9. /// <summary>
  10. /// 方法注释
  11. /// </summary>
  12. public string Comment { get; set; }
  13. }
  14. }
  15. Method Area

构造函数是这里

  1. using System;
  2. using System.CodeDom;
  3. using System.Collections.Generic;
  4. namespace CodeMacker
  5. {
  6. public class ConstructorEntity
  7. {
  8. /// <summary>
  9. /// 参数级
  10. /// </summary>
  11. public List<ParamInfo> Params { get; set; }
  12. /// <summary>
  13. /// 构造函数类型
  14. /// </summary>
  15. public MemberAttributes Attr { get; set; } = MemberAttributes.Public | MemberAttributes.Final;
  16. }
  17. public class ParamInfo
  18. {
  19. public ParamInfo(Type paramType, string name)
  20. {
  21. ParamType = paramType;
  22. Name = name;
  23. }
  24. /// <summary>
  25. /// 类型
  26. /// </summary>
  27. public Type ParamType { get; private set; }
  28. /// <summary>
  29. /// 名称
  30. /// </summary>
  31. public string Name { get; private set; }
  32. /// <summary>
  33. /// 赋值名称
  34. /// </summary>
  35. public string ReferenceName { get; private set; }
  36. }
  37. }
  38. Ctor Area

反射是这里

  1. using System.Reflection;
  2. namespace CodeMacker
  3. {
  4. public static class CodeMackerReflection
  5. {
  6. public static FieldInfo GetField(this object obj, string fieldName, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
  7. => obj.GetType().GetField(fieldName, bindingAttr);
  8. public static FieldInfo[] GetFields(this object obj, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
  9. => obj.GetType().GetFields(bindingAttr);
  10. public static PropertyInfo GetPropertyInfo(this object obj, string propertyName, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
  11. => obj.GetType().GetProperty(propertyName, bindingAttr);
  12. public static PropertyInfo[] GetPropertysInfo(this object obj, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
  13. => obj.GetType().GetProperties(bindingAttr);
  14. public static MethodInfo GetMethod(this object obj, string methodName, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
  15. => obj.GetType().GetMethod(methodName, bindingAttr);
  16. public static MethodInfo[] GetMethods(this object obj, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
  17. => obj.GetType().GetMethods(bindingAttr);
  18. }
  19. }
  20. Reflection Area

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

好像也写了不少,啊哈哈哈哈哈。自己骗自己。
重头戏就放到(下)吧
下一定是特别长的一章,嗯,一定是。
顺路再给自己创造个流量,虽然也不知道能干吗
————————————————
原文链接:https://www.cnblogs.com/SevenWang/p/15568027.html