-4

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
theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • This looks like a duplicate of: http://stackoverflow.com/questions/14479074/c-sharp-reflection-load-assembly-and-invoke-a-method-if-it-exists Maybe this is part of the problem too: http://stackoverflow.com/questions/6049332/i-cant-find-system-speech – Marksl Oct 29 '16 at 16:21
  • @Marksl I looked at that first question to get the code I have at the moment, but as you can see above, it is not working, so... – theonlygusti Oct 29 '16 at 16:32
  • Your question is being downvoted because it is a code dump followed by a null reference exception. There is no evidence of research on your part. – BJ Myers Oct 30 '16 at 02:55
  • @BJMyers for future, how am I meant to evidence that research? – theonlygusti Sep 10 '17 at 14:45

1 Answers1

1

Your code contains bug, you can't work with class if you specified incorrect namespace (neither via reflection nor without it)

You use incorrect namespace here (that's why you received null reference exception ):

System.Type type = assembly.GetType("System.Speech.SpeechSynthesizer");//type == null

Here is example of correct namespaces:

System.Type type = assembly.GetType("System.Speech.Synthesis.SpeechSynthesizer");

Update1: Another note. invoke returns a prompt, and you shouldn't exit the programm while asynchronous method is working (off course, only if you really want to listen speech to the end). I added few lines to your code to wait until speach will be finished:

internal class Program
{
    private static void Main(string[] args)
    {
        var assembly = Assembly.LoadFrom("System.Speech.dll");
        var type = assembly.GetType("System.Speech.Synthesis.SpeechSynthesizer");
        var methodinfo = type.GetMethod("SpeakAsync", new[] {typeof(string)});
        if (methodinfo == null) throw new Exception("No methodinfo.");

        var speechparameters = new object[1];
        speechparameters[0] = "+100"; // returns something like "+100"

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

        while (!prompt.IsCompleted)
        {
            Task.Delay(500).Wait();
        }
    }
}

Update 2

Make sure you have the correct language pack. MSDN

Update 3 If you use Mono, try to make sure that this feature should works on Mono. I gues there are some problems with Mono realization.

Maxim Kitsenko
  • 2,042
  • 1
  • 20
  • 43