samedi 29 octobre 2016

C# how to load assembly with reflection

I am trying to load an assembly, System.Speech, via reflection, so that I can use the SpeakAsync method to read aloud some text.

I wrote this:

System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom("System.Speech.dll");
System.Type type = assembly.GetType("System.Speech.SpeechSynthesizer");
var methodinfo = type.GetMethod("SpeakAsync", new System.Type[] {typeof(string)} );
if (methodinfo == null) throw new System.Exception("No methodinfo.");

object[] speechparameters = new object[1];
speechparameters[0] = GetVerbatim(text); // returns something like "+100"

var o = System.Activator.CreateInstance(type);
methodinfo.Invoke(o, speechparameters);

But get the error

System.NullReferenceException: Object reference not set to an instance of an object





Aucun commentaire:

Enregistrer un commentaire