refactor: Use generic collections in PrimitiveManager

The class that instantiates ViewStreams via reflection
This commit is contained in:
John Colvin committed 2024-11-29 15:05:53 +00:00
1 parent 1f41b0ff9f
commit ffb8943afa
2 files changed
+11 -15

No files matched your search

@@ -8,11 +8,12 @@ namespace BCG {
using System.Collections;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
public class PrimitiveManager {
private IDictionary myTypeLookupTable = new Hashtable();
private static PrimitiveManager mySelf;
private ArrayList myAssemblyNames = new ArrayList();
readonly List<string> myAssemblyNames = new();
readonly Dictionary<string, Type> myTypeLookupTable = new();
private PrimitiveManager() {
myLoadAllTypes();
@@ -100,15 +101,14 @@ namespace BCG {
}
/// <summary>
/// Returns the Type name corresponding to the primitive name
/// Returns the Type corresponding to the primitive name
/// </summary>
/// <param name="friendlyName"></param>
/// <returns></returns>
public string find(string friendlyName) {
if (myTypeLookupTable[friendlyName] == null)
throw new ApplicationException(friendlyName + " is not the friendly name of any of the loaded types");
return (myTypeLookupTable[friendlyName] as Type).Name;
}
public Type find(string friendlyName) =>
myTypeLookupTable.TryGetValue(friendlyName, out var t)
? t
: throw new ArgumentException($"'{friendlyName}' is not the friendly name of any of the loaded types", nameof(friendlyName));
/// <summary>
/// Constructs and returns a ViewStream of type corresponding to the friendly name
@@ -123,12 +123,8 @@ namespace BCG {
constructorArgs = new object[args.Count];
args.CopyTo(constructorArgs, 0);
}
if (myTypeLookupTable[friendlyName] == null)
throw new ApplicationException(friendlyName + " is not the friendly name of any of the loaded types");
return
constructorArgs == null ?
Activator.CreateInstance(myTypeLookupTable[friendlyName] as Type) as ViewStream :
Activator.CreateInstance(myTypeLookupTable[friendlyName] as Type, constructorArgs) as ViewStream;
var type = find(friendlyName);
return Activator.CreateInstance(type, constructorArgs) as ViewStream;
}
/// <summary>
@@ -20,7 +20,7 @@ public class PrimitiveExpression : StatementExpression {
}
public override string print(int startCol, IList<string> activeBindings) {
string prelude = "new " + PrimitiveManager.instance.find(myName) + " (";
string prelude = "new " + PrimitiveManager.instance.find(myName).Name + " (";
return prelude + myPrintArgs(startCol + prelude.Length, activeBindings) + ")";
}