Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我怎样才能一直捕捉异常,以允许我恢复我所做的一切';我在干什么?_C#_Exception_Reflection_Properties_Exception Handling - Fatal编程技术网

C# 我怎样才能一直捕捉异常,以允许我恢复我所做的一切';我在干什么?

C# 我怎样才能一直捕捉异常,以允许我恢复我所做的一切';我在干什么?,c#,exception,reflection,properties,exception-handling,C#,Exception,Reflection,Properties,Exception Handling,如果标题不够清楚,很抱歉。但基本上我有一个方法,通过反射复制组件 public static T GetCopyOf<T>(this Component comp, T other) where T : Component { Type type = comp.GetType(); if (type != other.GetType()) return null; // type mis-match BindingFlags flags = BindingFl

如果标题不够清楚,很抱歉。但基本上我有一个方法,通过反射复制
组件

public static T GetCopyOf<T>(this Component comp, T other) where T : Component
{
    Type type = comp.GetType();
    if (type != other.GetType()) return null; // type mis-match
    BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default;
    PropertyInfo[] pinfos = type.GetProperties(flags);
    foreach (var pinfo in pinfos) {
        if (pinfo.CanWrite)
            pinfo.SetValue(comp, pinfo.GetValue(other, null), null);
    }
    FieldInfo[] finfos = type.GetFields(flags);
    foreach (var finfo in finfos) {
        finfo.SetValue(comp, finfo.GetValue(other));
    }
    return comp as T;
}
在这种情况下,如果我得到这个异常,我想做的就是继续下一个属性(即忽略当前属性-不要中断)

那么,我如何捕捉这个异常,以允许我恢复复制其他属性的方式呢?我猜它必须是递归方法,因为可能有其他属性在其setter中抛出异常

另一种表达我想要的方式是
如果(有异常)继续


谢谢。

抱歉,出于某种原因,我没有想到:

    if (pinfo.CanWrite) {
        try {
            pinfo.SetValue(comp, pinfo.GetValue(other, null), null);
        }
        catch { }
    }

抱歉,出于某种原因,我没有想到:

    if (pinfo.CanWrite) {
        try {
            pinfo.SetValue(comp, pinfo.GetValue(other, null), null);
        }
        catch { }
    }

抱歉,出于某种原因,我没有想到:

    if (pinfo.CanWrite) {
        try {
            pinfo.SetValue(comp, pinfo.GetValue(other, null), null);
        }
        catch { }
    }

抱歉,出于某种原因,我没有想到:

    if (pinfo.CanWrite) {
        try {
            pinfo.SetValue(comp, pinfo.GetValue(other, null), null);
        }
        catch { }
    }

将例外情况如下所示:

if (pinfo.CanWrite)
{
    try
    {
        pinfo.SetValue(comp, pinfo.GetValue(other, null), null);
    }
    catch(System.NotImplementedException ex)
    { 
        //do nothing
    }
}

将例外情况如下所示:

if (pinfo.CanWrite)
{
    try
    {
        pinfo.SetValue(comp, pinfo.GetValue(other, null), null);
    }
    catch(System.NotImplementedException ex)
    { 
        //do nothing
    }
}

将例外情况如下所示:

if (pinfo.CanWrite)
{
    try
    {
        pinfo.SetValue(comp, pinfo.GetValue(other, null), null);
    }
    catch(System.NotImplementedException ex)
    { 
        //do nothing
    }
}

将例外情况如下所示:

if (pinfo.CanWrite)
{
    try
    {
        pinfo.SetValue(comp, pinfo.GetValue(other, null), null);
    }
    catch(System.NotImplementedException ex)
    { 
        //do nothing
    }
}

非常感谢。在我问过之后,我立即想到了做这件事。出于某种原因……嗯,我不知道为什么,但出于某种原因,像这样做似乎并没有真正抓住它——它仍然会断裂。我必须这样做,就像
捕获{}
知道为什么吗?可能是其他类型的异常
(除了System.NotImplementedException)
被抛出;在那种情况下,
catch{}
将起作用,我不这么认为。因为在控制台中,我仍然可以看到抛出了
NotImplementedException
的输出。谢谢。在我问过之后,我立即想到了做这件事。出于某种原因……嗯,我不知道为什么,但出于某种原因,像这样做似乎并没有真正抓住它——它仍然会断裂。我必须这样做,就像
捕获{}
知道为什么吗?可能是其他类型的异常
(除了System.NotImplementedException)
被抛出;在那种情况下,
catch{}
将起作用,我不这么认为。因为在控制台中,我仍然可以看到抛出了
NotImplementedException
的输出。谢谢。在我问过之后,我立即想到了做这件事。出于某种原因……嗯,我不知道为什么,但出于某种原因,像这样做似乎并没有真正抓住它——它仍然会断裂。我必须这样做,就像
捕获{}
知道为什么吗?可能是其他类型的异常
(除了System.NotImplementedException)
被抛出;在那种情况下,
catch{}
将起作用,我不这么认为。因为在控制台中,我仍然可以看到抛出了
NotImplementedException
的输出。谢谢。在我问过之后,我立即想到了做这件事。出于某种原因……嗯,我不知道为什么,但出于某种原因,像这样做似乎并没有真正抓住它——它仍然会断裂。我必须这样做,就像
捕获{}
知道为什么吗?可能是其他类型的异常
(除了System.NotImplementedException)
被抛出;在那种情况下,
catch{}
将起作用,我不这么认为。因为在控制台中,我仍然可以看到抛出了
NotImplementedException
的输出。