Skip to content

BHoM_Engine: Fix issues with system types from newer runtimes to older #3489

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

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
13 changes: 10 additions & 3 deletions BHoM_Engine/Create/Type/GenericType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ private static Type GenericTypeAngleBrackets(string name, bool silent = false, b
}
//Main type definition as string up until the first split char (first '<').
//Number of generic arguments will be 1 less than the number of argsSplit count
return GenericType(name.Substring(0, argsSplit[0]) + "`" + (argsSplit.Count - 1), arguments, silent, takeFirstIfMultiple);
Type type = GenericType(name.Substring(0, argsSplit[0]) + "`" + (argsSplit.Count - 1), arguments, silent, takeFirstIfMultiple);

if (type != null && name.Contains("&"))
type = type.MakeByRefType();
return type;
}

/***************************************************/
Expand Down Expand Up @@ -209,9 +213,12 @@ private static Type GenericTypeSquareBrackets(string name, bool silent = false,
arguments.Add(name.Substring(argsStarts[i], argsEnd[i] - argsStarts[i]).Trim());
}
string mainName = name.Substring(0, nameEnd);
//Main type definition as string up until the first split char (first '<').
//Main type definition as string up until the first split char (first '[[').
//Number of generic arguments will be 1 less than the number of argsSplit count
return GenericType(mainName, arguments, silent, takeFirstIfMultiple);
Type type = GenericType(mainName, arguments, silent, takeFirstIfMultiple);
if (type != null && name.Contains("&"))
type = type.MakeByRefType();
return type;
}

/***************************************************/
Expand Down
16 changes: 13 additions & 3 deletions BHoM_Engine/Create/Type/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ public static Type Type(string name, bool silent = false, bool takeFirstIfMultip
return null;
}

if (name.StartsWith("System.")) //If a system type, try create it before doing anything else. If failing, rest of method will handle unqualified, generics, reference etc
{
Type type = System.Type.GetType(name);
if (type != null)
return type;
}

if (name.Contains('<'))
return GenericTypeAngleBrackets(name, silent, takeFirstIfMultiple);
else if (name.Contains('`') && name.Contains("[["))
Expand All @@ -67,9 +74,9 @@ public static Type Type(string name, bool silent = false, bool takeFirstIfMultip
if (type == null)
type = System.Type.GetType(unQualifiedName); //Fallback for when deserialising a type from a later net runtime to a lower net runtime. Can be critical when going between softwares of different net runtimes.

if (type == null && name.EndsWith("&"))
if (type == null && name.Contains("&"))
{
type = Type(name.TrimEnd(new char[] { '&' }), true);
type = Type(name.Replace("&", ""), silent, takeFirstIfMultiple);
if (type != null)
type = type.MakeByRefType();
}
Expand Down Expand Up @@ -103,7 +110,10 @@ public static Type Type(string name, bool silent = false, bool takeFirstIfMultip
["System.Drawing.Bitmap"] = typeof(System.Drawing.Bitmap),
["System.Collections.Generic.SortedDictionary`2"] = typeof(System.Collections.Generic.SortedDictionary<,>),
["System.Data.DataTable"] = typeof(System.Data.DataTable),
["System.Collections.Generic.HashSet`1"] = typeof(System.Collections.Generic.HashSet<>)
["System.Collections.Generic.HashSet`1"] = typeof(System.Collections.Generic.HashSet<>),
["System.Xml.XmlNode"] = typeof(System.Xml.XmlNode),
["System.Xml.Linq.XDocument"] = typeof(System.Xml.Linq.XDocument),
["System.Collections.Concurrent.ConcurrentBag`1"] = typeof(System.Collections.Concurrent.ConcurrentBag<>)
};

/*******************************************/
Expand Down
10 changes: 2 additions & 8 deletions Serialiser_Engine/Compute/Deserialise/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static partial class Compute
/*******************************************/
/**** Private Methods ****/
/*******************************************/

private static Type DeserialiseType(this BsonValue bson, Type value, string version, bool isUpgraded)
{
// Handle the case where the type is represented as a string
Expand Down Expand Up @@ -146,16 +146,10 @@ private static Type GetTypeFromName(string fullName)
Type type = null;
if (fullName == "T")
return null;
if (fullName.IsOmNamespace())
type = Base.Create.Type(fullName, true, true);
else if (fullName.IsEngineNamespace())
type = Base.Create.EngineType(fullName, true, true);
else
{
type = Type.GetType(fullName);
if (type == null)
type = System.Type.GetType(Base.Query.UnqualifiedName(fullName));
}
type = Base.Create.Type(fullName, true, true); //Use for all cases except engine methods as method able to handle both oM, and system types, as well as adapter. Also this method is called no matter what if the type is serialised as a string rather than document in this file

if (type == null)
{
Expand Down