Skip to content

Instantly share code, notes, and snippets.

@sonoichi60
Last active August 11, 2018 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonoichi60/8079e280de7f09d5632e8db52c295819 to your computer and use it in GitHub Desktop.
Save sonoichi60/8079e280de7f09d5632e8db52c295819 to your computer and use it in GitHub Desktop.
型の構造を出力する拡張メソッド
using System.Reflection;
public static class ConstructorInfoExtensions
{
public static string ToCode(this ConstructorInfo info)
{
var str = "";
str += info.IsPublic ? "public " : (info.IsPrivate ? "private " : "protected ");
str += info.Name + "( ";
var prms = info.GetParameters();
for (int i = 0; i < prms.Length; i++)
{
var prm = prms[i];
str += prm.ParameterType.ToCodeName() + " ";
str += prm.Name + "";
str += prm.HasDefaultValue ? string.Format(" = {0}", prm.DefaultValue) : "";
str += (i == (prms.Length - 1) ? " " : ", ");
}
str += ");";
return str;
}
}
using System.Reflection;
public static class FieldInfoExtensions
{
public static string ToCode(this FieldInfo info)
{
var str = "";
str += info.IsPublic ? "public " : (info.IsPrivate ? "private " : "protected ");
str += info.IsLiteral ? "const " : (info.IsStatic ? "static " : "");
str += info.IsInitOnly ? "readonly " : "";
str += info.FieldType.ToCodeName() + " ";
str += info.Name;
str += info.IsLiteral ? string.Format(" = {0};", info.GetRawConstantValue()) : ";";
return str;
}
}
using System.Reflection;
public static class MethodInfoExtensions
{
public static string ToCode(this MethodInfo info)
{
var str = "";
str += info.IsPublic ? "public " : (info.IsPrivate ? "private " : "protected ");
str += info.IsStatic ? "static " : "";
var isOverride = info.GetBaseDefinition().DeclaringType != info.DeclaringType;
str += isOverride ? "override " : (info.IsAbstract ? "abstruct " : (info.IsVirtual ? "virtual " : ""));
str += info.ReturnType.ToCodeName() + " ";
str += info.Name + "( ";
var prms = info.GetParameters();
for (int i = 0; i < prms.Length; i++)
{
var prm = prms[i];
str += prm.ParameterType.ToCodeName() + " ";
str += prm.Name + "";
str += prm.HasDefaultValue ? string.Format(" = {0}", prm.DefaultValue) : "";
str += (i == (prms.Length - 1) ? " " : ", ");
}
str += ");";
return str;
}
}
using System.Linq;
using System.Reflection;
public static class PropertyInfoExtensions
{
public static string ToCode(this PropertyInfo info)
{
var acs = info.GetAccessors(true);
var acLv = acs.Any(ac => ac.IsPublic) ? 0 : (acs.Any(ac => ac.IsFamily) ? 1 : 2);
var str = "";
str += acLv == 0 ? "public " : (acLv == 1 ? "protected " : "private ");
str += acs[0].IsAbstract ? "abstruct " : (acs[0].IsVirtual ? "virtual " : "");
str += acs[0].GetBaseDefinition().DeclaringType != acs[0].DeclaringType ? "override " : "";
str += info.PropertyType.ToCodeName() + " ";
str += info.Name + " { ";
var get = info.GetGetMethod(true);
if (get != null)
str += string.Format("{0}{1}", (acLv <= 1 && get.IsPrivate) ? "private " : ((acLv == 0 && get.IsFamily) ? "protected " : ""), "get; ");
var set = info.GetSetMethod(true);
if (set != null)
str += string.Format("{0}{1}", (acLv <= 1 && set.IsPrivate) ? "private " : ((acLv == 0 && set.IsFamily) ? "protected " : ""), "set; ");
str += "}";
return str;
}
}
using System;
using System.Reflection;
using System.Collections.Generic;
public static class TypeExtensions
{
public static IEnumerable<Type> GetBaseTypes(this Type self)
{
for (var baseType = self.BaseType; null != baseType; baseType = baseType.BaseType)
{
yield return baseType;
}
}
public static string OutputStructure(this Type type, bool declaredOnly = false, bool ignoreSpecialName = true)
{
var bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;
if (declaredOnly)
bindingFlags |= BindingFlags.DeclaredOnly;
var code = "";
code += string.Format("===== {0} =====\n", type.FullName);
code += string.Format("declearedOnly: {0}\n", declaredOnly);
code += string.Format("ignoreSpecialName: {0}\n", ignoreSpecialName);
if (type.IsClass)
{
code += "_____ Interfaces _____\n";
var interfaces = type.GetInterfaces();
foreach (var interfase in interfaces)
{
code += interfase.Name + "\n";
}
code += "_____ BaseClasses _____\n";
var baseTypes = type.GetBaseTypes();
foreach (var baseType in baseTypes)
{
code += baseType.ToCodeName() + "\n";
}
}
code += "_____ Constructors _____\n";
var ctors = type.GetConstructors(bindingFlags);
foreach (var ctor in ctors)
{
code += ctor.ToCode() + "\n";
}
code += "_____ Fields _____\n";
var fields = type.GetFields(bindingFlags);
foreach (var field in fields)
{
if (ignoreSpecialName && (field.IsSpecialName || field.Name[0] == '<'))
continue;
code += field.ToCode() + "\n";
}
code += "_____ Properties _____\n";
var props = type.GetProperties(bindingFlags);
foreach (var prop in props)
{
if (ignoreSpecialName && prop.IsSpecialName)
continue;
code += prop.ToCode() + "\n";
}
code += "_____ Methods _____\n";
var methods = type.GetMethods(bindingFlags);
foreach (var method in methods)
{
if (ignoreSpecialName && method.IsSpecialName)
continue;
code += method.ToCode() + "\n";
}
return code;
}
public static string ToCodeName(this Type type)
{
if (type == typeof(int)) return "int";
if (type == typeof(float)) return "float";
if (type == typeof(bool)) return "bool";
if (type == typeof(long)) return "long";
if (type == typeof(short)) return "short";
if (type == typeof(uint)) return "uint";
if (type == typeof(ulong)) return "ulong";
if (type == typeof(ushort)) return "ushort";
if (type == typeof(void) || type == typeof(string) || type == typeof(object) || type == typeof(char) ||
type == typeof(double) || type == typeof(byte) || type == typeof(sbyte) || type == typeof(decimal))
return type.Name.ToLower();
return type.Name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment