【发布时间】:2015-02-07 00:04:45
【问题描述】:
我已经阅读了几篇关于如何使用反射获取类的文章,即使 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”的定义。
我正在尝试使用它,但我不确定如何更改它。 Reflection 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; }
}
}
稍后我想使用反射创建每个实体,方法如下:
public static async void CreateDatabase()
{
var profile = await ConnectionDb().CreateTableAsync<profile>();
var bloodresults = await ConnectionDb().CreateTableAsync<bloodResults>();
}
我为什么要这样做?因为这不是我第一次使用 SQLite,我想创建一个标准方法来让我的工作更轻松。感谢您的宝贵知识。
【问题讨论】:
-
旁注:由于命名空间与程序集没有正式关系(即与 Java 不同),因此您不能真正“获取命名空间中的所有类”,因为该集合不受任何约束……而且您可能的意思是“......来自这个命名空间中的这个程序集”。
标签: c# reflection windows-runtime windows-phone-8.1 win-universal-app