如何在 WP8.1 应用程序中使用反射获取命名空间中的所有 类?
How to get all classes in a Namespace using Reflection in a WP8.1 App?
我已经阅读了几篇关于如何使用反射获取 类 的帖子,即使在 Whosebug 中有不同的示例,但其中 none 与此版本的 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
这是我的 class:
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,所以我想创建一个标准方法来简化我的工作。感谢您的宝贵知识。
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));
我已经阅读了几篇关于如何使用反射获取 类 的帖子,即使在 Whosebug 中有不同的示例,但其中 none 与此版本的 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
这是我的 class:
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,所以我想创建一个标准方法来简化我的工作。感谢您的宝贵知识。
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));