❔ Why I need to specify the assembly while I was reflecting DataTable?

Why do I have to code like
Console.WriteLine(Type.GetType("System.Data.DataTable, System.Data.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"));
Console.WriteLine(Type.GetType("System.Data.DataTable, System.Data.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"));
but
Console.WriteLine(Type.GetType("System.Data.DataTable"));
Console.WriteLine(Type.GetType("System.Data.DataTable"));
18 Replies
thirteenbinary
thirteenbinaryOP2y ago
By the way, I have referenced relevant assemblies not excluding System.Data.Common
Pobiega
Pobiega2y ago
how about using typeof(System.Data.DataTable) instead?
thirteenbinary
thirteenbinaryOP2y ago
Just have a try and I don't want a alternative.
Kouhai
Kouhai2y ago
Well, in the docs it says If the type is in the currently executing assembly or in mscorlib.dll/System.Private.CoreLib.dll, it is sufficient to supply the type name qualified by its namespace.
Pobiega
Pobiega2y ago
which DataTable is not but regardless, I really don't think Type.GetType is what you should be using
thirteenbinary
thirteenbinaryOP2y ago
That's a piece of helpful advice.
Pobiega
Pobiega2y ago
Are you being funny? Kouhai just explained why it needs a fully qualified assembly, and I pointed out that DataTable doesnt meet the requirements for the short one regardless, string-based runtime reflection is... not recommended, ever, if avoidable use typeof if you just need the Type for DataTable
thirteenbinary
thirteenbinaryOP2y ago
yeah The "currently executing assembly" means my project?
Pobiega
Pobiega2y ago
more or less, yes.
thirteenbinary
thirteenbinaryOP2y ago
So only for some assemblies I don't need to specify the assembly.
Pobiega
Pobiega2y ago
why are you using GetType in the first place? this gives of XY problem vibes
Kouhai
Kouhai2y ago
I agree with Pobiega, it's better to use typeof, not reason to use GetType
thirteenbinary
thirteenbinaryOP2y ago
I just want to figure it out.
Pobiega
Pobiega2y ago
okay, then its because I can make a type called System.Data.DataTable myself its very easy
namespace System.Data;
public class DataTable
{
}
namespace System.Data;
public class DataTable
{
}
done. so because this is possible, you need to fully qualify WHAT assembly you mean and as you've noticied, this is annoying and error prone and needs to be updated if you ever update the nuget... its just terrible. this is why we avoid GetType, if possible typeof gets the Type at compile time, so you dont need to mess with this
thirteenbinary
thirteenbinaryOP2y ago
Surely annoying in package management So better to use typeof()
Pobiega
Pobiega2y ago
yep
thirteenbinary
thirteenbinaryOP2y ago
thank u
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server