// A bit clumsy but better than full-blown reflection use or an open set of Create* methods for each // type we support now or in the future protected virtual T CreateHierarchyElement <T> (HierarchyBase parent) where T : HierarchyElement { Type type = typeof(T); HierarchyBase ret = null; if (type == typeof(HierarchyNamespace)) { ret = new HierarchyNamespace(Context, parent as Hierarchy); } else if (type == typeof(HierarchyClass)) { ret = new HierarchyClass(Context, parent as HierarchyNamespace); } else if (type == typeof(HierarchyImplements)) { ret = new HierarchyImplements(Context, parent as HierarchyObject); } else if (type == typeof(HierarchyMethod)) { ret = new HierarchyMethod(Context, parent as HierarchyObject); } else if (type == typeof(HierarchyConstructor)) { ret = new HierarchyConstructor(Context, parent as HierarchyObject); } else if (type == typeof(HierarchyException)) { ret = new HierarchyException(Context, parent as HierarchyMethod); } else if (type == typeof(HierarchyTypeParameter)) { ret = new HierarchyTypeParameter(Context, parent as HierarchyElement); } else if (type == typeof(HierarchyTypeParameterGenericConstraint)) { ret = new HierarchyTypeParameterGenericConstraint(Context, parent as HierarchyTypeParameter); } else if (type == typeof(HierarchyMethodParameter)) { ret = new HierarchyMethodParameter(Context, parent as HierarchyMethod); } else if (type == typeof(HierarchyField)) { ret = new HierarchyField(Context, parent as HierarchyObject); } else if (type == typeof(HierarchyInterface)) { ret = new HierarchyInterface(Context, parent as HierarchyNamespace); } else if (type == typeof(HierarchyEnum)) { ret = new HierarchyEnum(Context, parent as Hierarchy); } else { throw new InvalidOperationException($"Unsupported hierarchy element type {type}"); } return(ret as T); }
// Walks up the parent chain and calculates the namespace based on the parent types instead of just // reading the full managed name which can lead to invalid results public virtual string GetNamespace() { if (Parent == null) { return(null); } HierarchyElement parent = ParentElement; HierarchyBase previousParent = parent; var segments = new List <string> (); while (parent != null) { var ns = parent as HierarchyNamespace; if (ns != null) { if (!(previousParent is HierarchyNamespace)) { throw new InvalidOperationException("Parent namespaces must not be interlaced with other types"); } segments.Add(ns.GetManagedName()); } previousParent = parent; parent = parent.ParentElement; } if (segments.Count == 0) { throw new InvalidOperationException($"A type must be a child of at least one namespace"); } return(String.Join(".", segments)); }
public HierarchyExternalTypeReference(GeneratorContext context, HierarchyBase parent, string typeName, bool isManaged = false) : base(context, parent) { if (String.IsNullOrEmpty(nameof(typeName))) { throw new ArgumentException("must not be null or empty", nameof(typeName)); } Name = typeName; IsManaged = isManaged; }
static string TypeToPrefix(HierarchyBase element) { if (element == null) { return("[null]"); } return(TypeToPrefix(element.GetType())); }
protected T CreateHierarchyElementInternal <T> (HierarchyBase parent) where T : HierarchyElement { T ret = CreateHierarchyElement <T> (parent); if (ret == null) { throw new InvalidOperationException($"Hierarchy element of type {typeof (T)} not created"); } return(ret); }
protected virtual string BuildFullName(string firstSegment, Func <HierarchyElement, string> getSegment) { if (String.IsNullOrEmpty(firstSegment)) { throw new ArgumentException("must not be null or empty", nameof(firstSegment)); } if (getSegment == null) { throw new ArgumentNullException(nameof(getSegment)); } var segments = new List <string> { }; if (SplitName(firstSegment, out List <string> split)) { segments.AddRange(split); } else { segments.Add(firstSegment); } HierarchyBase parent = Parent; while (parent != null) { var element = parent as HierarchyElement; if (element == null) { break; } string segment = getSegment(element); if (String.IsNullOrEmpty(segment)) { throw new InvalidOperationException("Element name segment must not be null or empty"); } if (SplitName(segment, out split)) { segments.AddRange(split); } else { segments.Add(segment); } parent = element.Parent; } // string dbg = String.Join (" → ", segments); // Logger.Debug ($"Full name segments for {Name} ({GetType ().FullName}): {dbg}"); return(MakeFullName(segments)); }
protected HierarchyObject(GeneratorContext context, HierarchyBase parent) : base(context, parent) { }
public HierarchyEnum(GeneratorContext context, HierarchyBase parent) : base(context, parent) { DoNotAddBaseTypes = true; }
protected HierarchyElement(GeneratorContext context, HierarchyBase parent) : base(context) { Parent = parent ?? throw new ArgumentNullException(nameof(parent)); IsBoundAPI = false; }
public HierarchyException(GeneratorContext context, HierarchyBase parent) : base(context, parent) { }
public HierarchyTypeParameterGenericConstraint(GeneratorContext context, HierarchyBase parent) : base(context, parent) { }
string TypeToName(HierarchyBase element) { if (element == null) { return("[null]"); } Type type = element.GetType(); if (type == typeof(HierarchyNamespace)) { return("Namespace"); } else if (type == typeof(HierarchyClass)) { return("Class"); } else if (type == typeof(HierarchyInterfaceInvoker)) { return("Class (invoker)"); } else if (type == typeof(HierarchyTypeMember)) { return("Type Member"); } else if (type == typeof(HierarchyImplements)) { return("Implements"); } else if (type == typeof(HierarchyMethod)) { return("Method"); } else if (type == typeof(HierarchyConstructor)) { return("Constructor"); } else if (type == typeof(HierarchyException)) { return("Exception"); } else if (type == typeof(HierarchyTypeParameter)) { return("Type Parameter"); } else if (type == typeof(HierarchyTypeParameterGenericConstraint)) { return("Type Parameter Generic Constraint"); } else if (type == typeof(HierarchyMethodParameter)) { return("Method Parameter"); } else if (type == typeof(HierarchyField)) { return("Field"); } else if (type == typeof(HierarchyInterface)) { return("Interface"); } else if (type == typeof(HierarchyEnum)) { return("Enum"); } else { return(element.GetType().ToString()); } }
public HierarchyImplements(GeneratorContext context, HierarchyBase parent) : base(context, parent) { }
public HierarchyMethodParameter(GeneratorContext context, HierarchyBase parent) : base(context, parent) { }