I have a code like shown below
public static Type ToType(Type sType)
{
Assembly assembly = Assembly.Load(SerializableType.AssemblyName);
type = assembly.GetType(sType.Name);
}
UPDATE
In UI if I set value for base class no issues because their assembly creating normal way, but if you set value for user defined class their assembly creating different way like shown below
It returns null
if the class name is not found, most likely because the Name
property of your type just returns the type name, and not the namespace name to qualify it. Make sure that your Name
property includes the namespace qualifying it as well.
According to the MSDN on Assembly.GetType(string name), it returns:
An object that represents the specified class, or Nothing if the class is not found.
Thus since you're getting null
, it couldn't find the type name, most likely reason is it's either misspelled, or you didn't prepend the type name with the namespace.
This method only searches the current assembly instance. The name parameter includes the namespace but not the assembly.
Or, it's also possible the case is wrong on the type name, there is a version of GetType()
that supports a bool
argument for a case-insensitive name compare as well.
p.s. The namespace is needed because the assembly name may not be an indicator of the namespace. That is, if I had a type in an assembly MySystem.MyClasses.DLL
, this does not mean that the type is necessarily in the MySystem.MyClasses
namespace.
The full MSDN page (always good to see what things throw/return) is here: Assembly.GetType Method(String)
It's evident the assembly exists (or it would return null
and you'd get a NullReferenceException
), so another possibility is you don't have the same version of the assembly you are expecting (i.e. the program using this code has a different version of the assembly then the code generating the data).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With