我阅读了几篇有关如何使用反射获取类的文章,即使StackOverflow中有不同的示例,但它们均与WP或Windows的此版本无关,如果您尝试使用这些代码,则它们均无效。这是我尝试的最后一个:
string @namespace = "Supernova.Entities";
var types = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && t.Namespace == @namespace)
.ToList();
types.ForEach(t => Console.WriteLine(t.Name.GetType()));
我希望有人能给我一个主意,因为当我尝试类似的操作时,VS总是告诉我:“ System.Reflection.Assembly”不包含“ GetExecutingAssembly”的定义。
我正在尝试使用它,但是我不确定如何更改它。反射WinRT
这是我的课:
namespace Supernova.Entities
{
public class profile
{
[PrimaryKey]
public string email { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
}
public class bloodResults
{
[PrimaryKey, AutoIncrement]
public int idbloodresult { get; set; }
public double result { get; set; }
}
}
稍后,我想使用Reflection和这样的方法创建我的每个实体:
public static async void CreateDatabase()
{
var profile = await ConnectionDb().CreateTableAsync<profile>();
var bloodresults = await ConnectionDb().CreateTableAsync<bloodResults>();
}
我为什么要这样做?因为这不是我第一次使用SQLite,所以我想创建一个标准方法来简化我的工作。感谢您的宝贵知识。
GetExecutingAssembly
在WinRT中不可用,但是您可以使用它typeof(AClassInYourAssembly).GetTypeInfo().Assembly
。
string @namespace = "Supernova.Entities";
var assembly = typeof(YourClass).GetTypeInfo().Assembly;
var types = assembly.GetTypes()
.Where(t => t.GetTypeInfo().IsClass && t.Namespace == @namespace)
.ToList();
types.ForEach(t => Console.WriteLine(t.Name));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句