Some types cannot be obtained via Type.GetType
There was similar feedback in #6 , but now a new unrecognized type has been discovered.
methods of the interface: List<T> GetAll(DbTransaction trans = null); unobtainable type:DbTransaction ParameterTypeName:System.Data.Common.DbTransaction,System.Data
Type.GetType("System.Data.Common.DbTransaction,System.Data")
will eventually return null thus throwing an exception
Is there a good way to solve this problem?
I seem to have found the cause of the problem.
Type.GetType("System.Data.Common.DbTransaction,System.Data.Common")
This gets the type correctly.
After the comma is the namespace of the class.
So changing parameterInfo.ParameterType.Assembly.GetName().Name to parameterInfo.ParameterType.Namespace in your 732e686 change should do the trick.
"After the comma is the namespace of the class." No, the thing after the comma is the assembly name.
But the problem is, DbTransaction exist in different assemblies in different .NET versions. In .NET framework 4.8 it is in System.Data.dll while in .NET 6 it is in System.Data.Common.dll.
BTW: instead of manually building a full type name, maybe just use AssemblyQualifiedName instead?
@gdalsnes Thanks for your guidance, my current runtime environment is .NET4.8, The result of running the code below makes me more confused.
Type.GetType("System.Data.Common.DbTransaction,System.Data")
null was output.
Type.GetType("System.Data.Common.DbTransaction,System.Data.Common")
The correct result is output.
This is so weird, at least in my 4.8 project, the correct Type is output when using System.Data.Common. So what exactly determines the choice between System.Data.Common or System.Data?
I tested various things, only these 2 works (.net 4.6.1): Type.GetType("System.Data.Common.DbTransaction, System.Data.Common"); Type.GetType("System.Data.Common.DbTransaction, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
In 4.8 only this works for me: Type.GetType("System.Data.Common.DbTransaction, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
Possibly Microsoft has made some tricks with this type and moved it, "forwarded" it or something strange. It's bloody confusing at least.
In .net 5:
var t1 = Type.GetType("System.Data.Common.DbTransaction, System.Data"); var t3 = Type.GetType("System.Data.Common.DbTransaction"); var t2 = Type.GetType("System.Data.Common.DbTransaction, System.Data.Common"); var t4 = Type.GetType("System.Data.Common.DbTransaction, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); var t5 = Type.GetType("System.Data.Common.DbTransaction, System.Data, Version=4.0.0.0"); var t6 = Type.GetType("System.Data.Common.DbTransaction, System.Data, Version=4.0.0.0, Culture=neutral");
All work except t3.
The assembly qualified name you tried also worked for me, so wouldn't it be more generic if we changed the ParameterTypeName = parameterInfo.ParameterType.FullName + "," + parameterInfo.ParameterType.Assembly.GetName().Name, to ParameterTypeName = parameterInfo.ParameterType .AssemblyQualifiedName, in 732e686?
To pass database transaction over the wire, System.Transactions.Transaction (TransactionScope) should be used.