[英]Activator.CreateInstance(type) Throws Exception
實際上這是一個非常奇怪的異常,因為它只在我將項目構建為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.Private.Reflection.Execution.dll中出現“System.MissingMethodException”類型的異常,但未在用戶代碼中處理
其他信息:MissingConstructor_Name,Windows.UI.Xaml.Controls.RelativePanel。 有關詳細信息,請訪問http://go.microsoft.com/fwlink/?LinkId=623485
我發現這個異常的一些相關的問題,但他們都指向缺少的庫或類似更新庫本 ,但在我的應用程序沒有任何改變。
此問題與UWP應用程序的發布版本使用.NET本機工具鏈有關 。 在這種模式下, 反射需要一些提示才能正常工作。 顯然, RelativePanel
的構造函數不可用於反射。
幸運的是,有一個解決方法,如本博文中所述 。
在UWP項目的Properties文件夾中有一個名為default.rd.xml
的文件。 打開它並在<Applications>
元素中添加以下行:
<Type Name="Windows.UI.Xaml.Controls.RelativePanel"
Dynamic="Required All" Activate="Required All" />
Dynamic
屬性應該確保可以反射, Activate
屬性應該確保構造函數可用於激活 - 這對於您的情況是關鍵。
這應該包括RelativePanel
所有成員用於反射,一切都應該按預期工作。
您可以在此處查看 default.rd.xml
文件結構的更多詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.