Skip to content

Type matching based on unqualified names enabled in Create.EngineType #3389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion BHoM_Engine/Compute/ExtractAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static void ExtractTypes(Assembly asm)
{
if (!type.IsAutoGenerated())
{
if (type.Namespace.IsOmNamespace())
if (type.Namespace.IsEngineNamespace())
Global.EngineTypeList.Add(type);

if (type.Namespace != null)
Expand Down
20 changes: 14 additions & 6 deletions BHoM_Engine/Create/Type/EngineType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,36 @@ public static partial class Create
/**** Public Methods ****/
/***************************************************/

[PreviousVersion("7.3", "BH.Engine.Base.Create.EngineType(System.String, System.Boolean)")]
[Description("Creates an Engine type that matches the given name.")]
[Input("name", "Name to be searched for among all Engine types.")]
[Input("silent", "If true, the error about no type found will be suppressed, otherwise it will be raised.")]
[Input("takeFirstIfMultiple", "Defines what happens in case of finding multiple matching types. If true, first type found will be returned, otherwise null.")]
[Output("type", "BHoM Engine type that matches the given name.")]
public static Type EngineType(string name, bool silent = false)
public static Type EngineType(string name, bool silent = false, bool takeFirstIfMultiple = false)
{
if (string.IsNullOrWhiteSpace(name))
{
Compute.RecordError("Cannot create a type from a null string.");
return null;
}

List<Type> methodTypeList = Query.EngineTypeList();

List<Type> types = methodTypeList.Where(x => x.AssemblyQualifiedName.StartsWith(name)).ToList();
// Try finding any types based on the assembly qualified name
List<Type> types = Global.EngineTypeList.Where(x => x.AssemblyQualifiedName == name).ToList();

// If not found, look also based on unqualified name
if (types.Count == 0)
{
string unQualifiedName = name.Contains(",") ? name.Split(',').First() : name;
types = Global.EngineTypeList.Where(x => x.FullName == unQualifiedName).ToList();
}

if (types.Count == 1)
if (types.Count == 1 || (takeFirstIfMultiple && types.Count > 1))
return types[0];
else
{
//Unique method not found in list, check if it can be extracted using the system Type
Type type = System.Type.GetType(name, silent);
Type type = System.Type.GetType(name, false);
if (type == null && !silent)
{
if (types.Count == 0)
Expand Down
2 changes: 1 addition & 1 deletion Serialiser_Engine/Compute/Deserialise/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private static Type GetTypeFromName(string fullName)
if (fullName.IsOmNamespace())
type = Base.Create.Type(fullName, true, true);
else if (fullName.IsEngineNamespace())
type = Base.Create.EngineType(fullName, true);
type = Base.Create.EngineType(fullName, true, true);
else
type = Type.GetType(fullName);

Expand Down
Loading