【发布时间】:2017-02-12 08:38:26
【问题描述】:
其实这是一个很奇怪的异常,因为只有当我将项目构建为Release时才会发生,而当我选择Debug时根本不会发生。在调试模式下,应用运行良好,以下代码运行良好。
这是我的扩展方法的代码:
public static T DeepClone<T>(this T source) where T : UIElement
{
T result;
// Get the type
Type type = source.GetType();
// Create an instance
result = Activator.CreateInstance(type) as T; //throws exception here only if I build project as (release)
CopyProperties<T>(source, result, type);
DeepCopyChildren<T>(source, result);
return result;
}
例外情况是:
“System.MissingMethodException”类型的异常发生在 System.Private.Reflection.Execution.dll 但未在用户中处理 代码
附加信息:MissingConstructor_Name, Windows.UI.Xaml.Controls.RelativePanel。如需更多信息,请访问 http://go.microsoft.com/fwlink/?LinkId=623485
我发现了一些与此异常相关的问题,但它们都指向缺少库或更新库,例如 this,但没有更改我的应用程序中的任何内容。
【问题讨论】:
-
不相关,但当结果可能不是
T类型时,as T很有用。在功能方面,x as T表示x is T ? (T) x : null,除了x只评估一次。你知道x is T必然总是正确的,并且万一你在某处犯了错误,立即获得异常比CopyProperties内部某处的NullReferenceException更容易调试。因此,我推荐(T) Activator.CreateInstance(type)。
标签: c# uwp windows-10-universal .net-native