通过methodInfo.Invoke调用方法时,如果抛出异常,它似乎不会传播到我的catch块.
object value;
try
{
value = myMethod.Invoke(null, parameters);//program crashes with uncaught exception
}
catch
{
throw new Exception("Caught!");//never executed
}
Run Code Online (Sandbox Code Playgroud)
这个方法引发的特殊例外是KeyNotFoundException,但这应该不重要,因为我抓住了一切正确的东西?
我从Visual Studio获得的特定错误消息是
KeyNotFoundException was unhandled by user code
Run Code Online (Sandbox Code Playgroud)
而通常消息会说
KeyNotFoundException was unhandled
Run Code Online (Sandbox Code Playgroud)
如果调用不是反射调用.
我可以让方法检查它们是否在那里,如果没有返回null,但是使用异常处理似乎更可取.有没有办法从反射方法调用中传播异常?
当存在内存损坏时,有没有办法压制glibc生成的输出?这就是我所看到的
make
*** glibc detected *** /home/myname/php/sapi/cli/php: free(): invalid pointer: x0045d67f ***
======= Backtrace: =========
/lib/libc.so.6(+0x6eb41)[0x380b41]
<snip>
======= Memory map: ========
00115000-00116000 r-xp 00000000 00:00 0 [vdso]
001d7000-001ee000 r-xp 00000000 ca:01 540738 /lib/libpthread-2.12.2.so
001ee000-001ef000 r--p 00016000 ca:01 540738 /lib/libpthread-2.12.2.so
001ef000-001f0000 rw-p 00017000 ca:01 540738 /lib/libpthread-2.12.2.so
<snip>
Run Code Online (Sandbox Code Playgroud)
对于我正在做的工作,我不在乎这个信息,只关注make没有成功(返回值!= 0).这些消息正在填满屏幕,它使我的输出的其余部分不可读.我试过了:
make &> /dev/null
{ make ; } &> /dev/null
x=`make 2>&1` &> /dev/null
Run Code Online (Sandbox Code Playgroud)
但他们都没有抓住产量.如果它没有被写入stderr,它来自哪里?如果可能的话,我想要一个不需要重建glibc的解决方案.
这里有一些代码会给出这样的错误信息,但请注意这与我正在处理的代码(php源代码)无关.我只是想从我的控制台沉默这种类型的输出.
int main()
{
char* ptr = (char*)malloc(sizeof("test"));
char array[]= "test";
ptr = array;
free(ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 如果我想编写一个方法,它采用可变数量的"TDerived",其中TDerived是类"Base"的任何子类,有没有办法做到这一点?
以下代码仅适用于单个特定的指定子类:
void doStuff<TDerived>(params TDerived[] args) where TDerived : Base
{
//stuff
}
Run Code Online (Sandbox Code Playgroud)
即如果我有
class Super { }
class Sub0 : Super { }
class Sub1 : Super { }
Run Code Online (Sandbox Code Playgroud)
那我就做不到了
Sub0 s0 = new Sub0();
Sub1 s1 = new Sub1();
doStuff(s0, s1);
Run Code Online (Sandbox Code Playgroud)
因为我得到"最好的重载匹配......有一些无效的论点".
无论编译器如何处理类型约束和可变参数函数,这似乎(据我所知)完全类型安全.我知道我可以施放,但如果这是类型安全的,为什么不允许它?
编辑:
也许是一个更有说服力的例子:
void doStuff<TDerived>(params SomeReadOnlyCollection<TDerived>[] args) where TDerived : Base
{
foreach(var list in args)
{
foreach(TDerived thing in list)
{
//stuff
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Gtk.Image 小部件上设置单个像素。文档指出 Gtk.Image 的 ImageProp 属性返回一个 Gdk.Image 似乎可以让您编辑单个像素,但是每当我使用它时它只返回空值。
到目前为止,我的解决方案是从磁盘加载图像作为 System.Drawing.Bitmap,对其进行编辑,将其保存到一个临时文件,然后将其加载回 Gtk.Image,但这显然并不理想。
为了
Gtk.Image image = new Gtk.Image("images/test.png");
Gdk.Image gdkImage = image.ImageProp;
Run Code Online (Sandbox Code Playgroud)
为什么 gdkImage 总是为空?
图像本身加载并正确显示。
如果一个Sub类调用它的基类Super的构造函数,有没有办法告诉它不仅仅是对Super的构造函数的调用呢?
我正在将数据从磁盘加载到对象中,我想验证使用反射所有字段都不为空.但是,如果我调用我的基本构造函数,那么在所有构造函数完成之前检查它是不合适的.例:
class Super
{
string s;
float? f;
public Super(List<string> lines)
{
//initialize s and f based on lines
if (notFromSubclassConstructor))
{
AssertInitialized(this);
}
}
public static void AssertInitialized(Super super)
{
// Iterate through every field and make sure it isnt null
// I already know how to do this part. If we find a null
// field we throw an exception.
}
}
class Sub : Super
{
string something;
int? somethingElse;
public Sub(List<string> lines)
: base(lines) //we …Run Code Online (Sandbox Code Playgroud)