2014-12-13 28 views
0

我想要檢索使用反射從頁面對象繼承的類型列表。WinRT中的反射IsAssignableFrom

我有以下對象:

public sealed partial class Register : Page 
{ .... } 

在另一類,我使用:

var currentAssembly = this.GetType().GetTypeInfo().Assembly; 
var page = currentAssembly.DefinedTypes.Single(t => t.Name == p.PageName); 

var inherits = page.IsAssignableFrom(typeof(Page).GetTypeInfo()); 

其中p.PageName =註冊

出於某種原因,繼承是假的時它應該是真的。寄存器的定義和我所反映的類的定義都在同一個項目中。

回答

0

我能夠通過使用來解決該問題如下:

var pageInstance = (Page) Activator.CreateInstance(page.AsType()); 
if (pageInstance == null) 
{ 
    _logger.Debug(string.Format("The type {0} does not inherit from Page and cannot be navigated to.", page.Name)); 
    throw new ArgumentException("Only object names deriving from Page can be navigated to."); 
} 

我會歡迎一個更好的建議,這雖然。謝謝。