我試圖通過反射加載程序集,System.Speech
,以便我可以使用SpeakAsync
方法朗讀一些文本。C#如何通過反射加載程序集
我寫了這個:
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);
但得到的錯誤
System.NullReferenceException: Object reference not set to an instance of an object
這看起來像一個副本: http://stackoverflow.com/questions/14479074/c-sharp-reflection-load-assembly-and-invoke-a-method-if-it-exists 也許這是部分問題太:http://stackoverflow.com/questions/6049332/i-cant-find-system-speech – Marksl
@Marksl我看着第一個問題,以獲得我現在的代碼,但你可以見上面,它不工作,所以... – theonlygusti
我發誓,有人真的恨我,只是低估了我所有的問題。嚴重的是,這有什麼問題? – theonlygusti