Skip to content

Instantly share code, notes, and snippets.

@Ch3shireDev
Created November 19, 2019 08:57
Show Gist options
  • Select an option

  • Save Ch3shireDev/011c74f95ad95d39e485b97cbcb39474 to your computer and use it in GitHub Desktop.

Select an option

Save Ch3shireDev/011c74f95ad95d39e485b97cbcb39474 to your computer and use it in GitHub Desktop.
Reflections - class structure
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using FrPluginImporter.SadUe;
using SAD;
namespace XMLStructure
{
internal class Program
{
private static void Main(string[] args)
{
var sad = new DocFSad();
//ShowClassStructure(typeof(SadUe));
//ShowClassStructure(typeof(DocFSad));
ShowXmlStructure();
//Console.ReadKey();
}
private static void ShowClassStructure(Type elementType, string path = "")
{
if (path != "")
path = path + "." + elementType.Name;
else
path = elementType.Name;
Console.WriteLine($@"{path} {elementType}");
if (elementType.IsPrimitive || elementType.IsGenericType)
{
return;
}
foreach (var field in elementType.GetFields())
{
if (field.Name == "Parent") continue;
var type = field.FieldType;
Console.WriteLine($@"{path}.{field.Name} {type}");
if (!type.IsClass || type == typeof(string))
{
continue;
}
var childPath = path + "." + field.Name;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
{
var childType = type.GetGenericArguments().Single();
ShowClassStructure(childType, childPath);
continue;
}
if (type.IsClass)
{
ShowClassStructure(type, childPath);
}
}
foreach (var field in elementType.GetProperties())
{
if (field.Name == "Parent") continue;
var type = field.PropertyType;
if (!type.IsClass || type == typeof(string))
{
Console.WriteLine($@"{path}.{field.Name} {type}");
continue;
}
var childPath = path + "." + field.Name;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
{
var childType = type.GetGenericArguments().Single();
ShowClassStructure(childType, childPath);
continue;
}
if (type.IsClass)
{
ShowClassStructure(type, childPath);
}
}
}
private static void ShowXmlStructure(string filePath = "SadUe.xml")
{
var data = File.ReadAllText(filePath);
var xml = new XmlDocument();
xml.LoadXml(data);
ShowXmlChildren(xml.LastChild);
}
private static void ShowXmlChildren(XmlNode parentNode, string parentPath = "")
{
var path = $@"{parentPath}/{parentNode.Name}";
Console.WriteLine();
if (!string.IsNullOrEmpty(parentNode.Value)) Console.WriteLine($@"{path} {parentNode.Value}");
if (parentNode.Attributes != null)
foreach (XmlAttribute attribute in parentNode.Attributes)
Console.WriteLine($"{path}/@{attribute.Name}\t{attribute.Value}");
foreach (XmlNode childNode in parentNode.ChildNodes) ShowXmlChildren(childNode, path);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment