Skip to content

Commit

Permalink
Check for TypeLoader.initialize correctly (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock authored Aug 19, 2024
1 parent 03a5706 commit af1d7df
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/Speckle.Sdk/Host/TypeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ public static void Initialize(params Assembly[] assemblies)
}
}

public static Type GetType(string fullTypeString) =>
s_cachedTypes.GetOrAdd(
private static void CheckInitialized()
{
if (!s_initialized)
{
throw new InvalidOperationException("TypeLoader is not initialized.");
}
}

public static Type GetType(string fullTypeString)
{
CheckInitialized();
return s_cachedTypes.GetOrAdd(
fullTypeString,
typeString =>
{
Expand All @@ -41,9 +51,12 @@ public static Type GetType(string fullTypeString) =>
return type;
}
);
}

public static string GetFullTypeString(Type type) =>
s_fullTypeStrings.GetOrAdd(
public static string GetFullTypeString(Type type)
{
CheckInitialized();
return s_fullTypeStrings.GetOrAdd(
type,
t =>
{
Expand All @@ -52,6 +65,7 @@ public static string GetFullTypeString(Type type) =>
{
return nameof(Base);
}
Type? myType = t;
do
Expand All @@ -63,17 +77,21 @@ public static string GetFullTypeString(Type type) =>
{
throw new InvalidOperationException($"Type {t} is not registered with TypeLoader");
}
bases.Push(typeString);
}
myType = myType.BaseType;
} while (myType is not null && myType.Name != nameof(Base));
return string.Join(":", bases);
}
);
}

public static string? GetTypeString(Type type)
{
CheckInitialized();
var typeInfo = s_availableTypes.FirstOrDefault(tp => tp.Type == type);
if (typeInfo != null)
{
Expand All @@ -84,6 +102,7 @@ public static string GetFullTypeString(Type type) =>

public static Type GetAtomicType(string objFullType)
{
CheckInitialized();
var objectTypes = objFullType.Split(':').Reverse();
foreach (var typeName in objectTypes)
{
Expand Down

0 comments on commit af1d7df

Please sign in to comment.