- Home /
System.Type.GetType() doesn't work on WEBGL
Trying to get type from running assembly by full qualified type name:
var dataType = Type.GetType(typeName);
if (dataType == null)
{
throw new NullReferenceException("unknown type: " + typeName);
}
It works fine when show exceptions option (in project settings) is setted to "Full" but don't work when exceptions disabled. Is there any way to get type by name or I need to map types manual for compile time linkage?
Answer by JoshPeterson · Jun 19, 2015 at 10:47 AM
For WebGL, the correct behavior of Type.GetType
does require exception support to be enabled.
In order for System.Type.GetType(string)
to work, we need to know something about the calling assembly to look for the type. In order to traverse the call stack to find the calling method (and therefore the calling assembly), we need call stack information. By default, call stack information is not generated for WebGL builds unless exception support is enabled, as it takes up some space and time. So without call stack information, we cannot support System.Type.GetType(string)
correctly.
You can help WebGL though by telling it which assembly to use to find the type, then things work well. If you know the name of the assembly, you can also do something like this:
Assembly.Load("Assembly-CSharp").GetType("TestClass");
If you don't know the assembly name, you will need to perform some manual step to make this work.
Thank you. I had to combine this with Unity's link.xml file so WebGL build doesn't strip the assembly away. Also using fully qualified type name in Type.GetType works equally well e.g.:
Type.GetType("TestClass, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, Public$$anonymous$$eyToken=null")
Your answer
![](https://koobas.hobune.stream/wayback/20220613201131im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Error occurs while building for WebGL 0 Answers
Can't build WebGL because of IL2CPP crash 4 Answers
WebGL + Sqlite3 1 Answer