Version: (using KDE 4.3.3) OS: Linux Installed from: Ubuntu Packages When the constructor for QApplication is called through NUnit (for example, when initialising Qt in TestFixtureSetup to test dialogs), it throws a NullReferenceException. This is caused by System.Reflection.Assembly.GetEntryAssembly() returning null. To work around this, System.Reflection.Assembly.GetExecutingAssembly() could be called when the call to GetEntryAssembly fails. A patch is included that provides for this. Also, somewhat redundant code has been factored out. === modified file 'csharp/qyoto/gui/QApplicationExtras.cs' --- csharp/qyoto/gui/QApplicationExtras.cs 2009-11-21 11:39:37 +0000 +++ csharp/qyoto/gui/QApplicationExtras.cs 2009-11-21 11:53:55 +0000 @@ -6,24 +6,34 @@ using System.Text; public partial class QApplication : QCoreApplication, IDisposable { + + string[] GenerateArgs(string[] argv) + { + string[] args = new string[argv.Length + 1]; + Assembly a = System.Reflection.Assembly.GetEntryAssembly(); + + if(a == null) + a = System.Reflection.Assembly.GetExecutingAssembly(); + + object[] attrs = a.GetCustomAttributes(typeof(AssemblyTitleAttribute), false); + if (attrs.Length > 0) { + args[0] = ((AssemblyTitleAttribute) attrs[0]).Title; + } else { + QFileInfo info = new QFileInfo(a.Location); + args[0] = info.BaseName(); + } + argv.CopyTo(args, 1); + + return args; + } public QApplication(string[] argv) : this((Type) null) { CreateProxy(); Qt.qApp = this; - - string[] args = new string[argv.Length + 1]; - Assembly a = System.Reflection.Assembly.GetEntryAssembly(); - object[] attrs = a.GetCustomAttributes(typeof(AssemblyTitleAttribute), false); - if (attrs.Length > 0) { - args[0] = ((AssemblyTitleAttribute) attrs[0]).Title; - } else { - QFileInfo info = new QFileInfo(a.Location); - args[0] = info.BaseName(); - } - argv.CopyTo(args, 1); + + string[] args = GenerateArgs(argv); - interceptor.Invoke( "QApplication$?", - "QApplication(int&, char**)", + interceptor.Invoke( "QApplication$?", "QApplication(int&, char**)", typeof(void), typeof(int), args.Length, typeof(string[]), args ); SetupEventReceiver(); } @@ -32,40 +42,20 @@ CreateProxy(); Qt.qApp = this; - string[] args = new string[argv.Length + 1]; - Assembly a = System.Reflection.Assembly.GetEntryAssembly(); - object[] attrs = a.GetCustomAttributes(typeof(AssemblyTitleAttribute), false); - if (attrs.Length > 0) { - args[0] = ((AssemblyTitleAttribute) attrs[0]).Title; - } else { - QFileInfo info = new QFileInfo(a.Location); - args[0] = info.BaseName(); - } - argv.CopyTo(args, 1); + string[] args = GenerateArgs(argv); - interceptor.Invoke( "QApplication$?", - "QApplication(int&, char**)", + interceptor.Invoke( "QApplication$?", "QApplication(int&, char**)", typeof(void), typeof(int), args.Length, typeof(string[]), args, typeof(bool), GUIenabled ); SetupEventReceiver(); } - + public QApplication(string[] argv, QApplication.TypeOf arg3) : this((Type) null) { CreateProxy(); Qt.qApp = this; - string[] args = new string[argv.Length + 1]; - Assembly a = System.Reflection.Assembly.GetEntryAssembly(); - object[] attrs = a.GetCustomAttributes(typeof(AssemblyTitleAttribute), false); - if (attrs.Length > 0) { - args[0] = ((AssemblyTitleAttribute) attrs[0]).Title; - } else { - QFileInfo info = new QFileInfo(a.Location); - args[0] = info.BaseName(); - } - argv.CopyTo(args, 1); + string[] args = GenerateArgs(argv); - interceptor.Invoke( "QApplication$?", - "QApplication(int&, char**)", + interceptor.Invoke( "QApplication$?", "QApplication(int&, char**)", typeof(void), typeof(int), args.Length, typeof(string[]), args, typeof(QApplication.TypeOf), arg3 ); SetupEventReceiver(); }
Hi, could you please post your patch as an attachment? All the formatting and many non-linebreaks go lost when posting it in-line with the rest of the text,
Created attachment 38641 [details] Proposed patch My bad, here's the attachment (I could not add one when creating the bug but I now realise this would have worked too).
Applied in r1055893. Thanks for the patch!
You're welcome, thanks for applying it :)