Question

I am trying to create an instance of System.Diagnostics.ConsoleTraceListener using a string, which I am fetching from xml file.

Dictionary<string, TraceListener> dListeners = new Dictionary<string, TraceListener>();
string sType = "System.Diagnostics.ConsoleTraceListener";
Type oType = Type.GetType(sType);//getting null here
dListeners["Listener1"] = (TraceListener)Activator.CreateInstance(oType);

I am getting oType as null above. I can't figure out why, something missing ? Do I need to specify assembly name as well in the string ?

Was it helpful?

Solution

I was able to create the instance after specifying fully qualified name of the assembly.

Thanks @Hans

Dictionary<string, TraceListener> dListeners = new Dictionary<string, TraceListener>();
string sType = "System.Diagnostics.ConsoleTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
Type oType = Type.GetType(sType);              
dListeners["Listener1"] = (TraceListener)Activator.CreateInstance(oType);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top