Tag: reflection

如何在没有Application.Run的情况下从VBE加载项运行宏?

我正在为VBE编写COM加载项,其中一个核心function是在单击命令栏按钮时执行现有的VBA代码。 代码是用户在标准(.bas)模块中编写的unit testing代码,其模块如下所示: Option Explicit Option Private Module ‘@TestModule Private Assert As New Rubberduck.AssertClass ‘@TestMethod Public Sub TestMethod1() ‘TODO: Rename test On Error GoTo TestFail ‘Arrange: ‘Act: ‘Assert: Assert.Inconclusive TestExit: Exit Sub TestFail: Assert.Fail “Test raised an error: #” & Err.Number & ” – ” & Err.Description End Sub 所以我有这个代码获取主机Application对象的当前实例: protected HostApplicationBase(string applicationName) { Application […]

entity framework – 按名称获取实体

我有以下代码(示例): public dynamic GetData(string name) { using(var ctx = GetObjectContext()) { switch (name) { case “entity1”: return ctx.entity1.ToList(); case “entity2”: return ctx.entity2.ToList(); …… default: return null; } } } 我想避免在此示例中切换。 如何按名称查找所需的实体类,调用ToList()方法并返回数据? 我可以用reflection做到这一点吗? 你能帮助我吗?

使用reflection从属性Name获取lambda表达式

我想让用户选择不同属性的搜索。 例如 [输入文本] | [选择选项{ID,NAME,PHONE}] | [搜索] 然后我会像这样构建我的查询: repository.Where(lambda-expression) 从所选选项{ID,NAME,PHONE}构建lambda表达式(例如:x => x.NAME.Equals(INPUT TEXT)) 有没有办法从Property属性名称构建lambda,也许使用reflection? 谢谢

如何通过reflection获取接口基类型?

public interface IBar {} public interface IFoo : IBar {} typeof(IFoo).BaseType == null 我怎样才能得到IBar?

反思 – 获取属性名称

我想在不使用魔术字符串的情况下将属性名称传递给函数。 就像是: Get(x=>x.Property1); 其中Property1是ObjectType类型的属性。 方法实现会是什么样的?

reflection性能 – 创建代理(属性C#)

我在使用reflection时遇到了性能问题。 所以我决定为我的对象的属性创建委托,到目前为止得到了这个: TestClass cwp = new TestClass(); var propertyInt = typeof(TestClass).GetProperties().Single(obj => obj.Name == “AnyValue”); var access = BuildGetAccessor(propertyInt.GetGetMethod()); var result = access(cwp); static Func BuildGetAccessor(MethodInfo method) { var obj = Expression.Parameter(typeof(object), “o”); Expression<Func> expr = Expression.Lambda<Func>( Expression.Convert( Expression.Call( Expression.Convert(obj, method.DeclaringType), method), typeof(object)), obj); return expr.Compile(); } 结果非常令人满意,比使用传统方法快3-40-40倍( PropertyInfo.GetValue (obj, null); ) 问题是: 如何制作属性的SetValue ,其工作方式相同? […]

如何从C#中的对象实例获取自定义属性

假设我有一个名为Test的类,其中一个名为Title的属性具有自定义属性: public class Test { [DatabaseField(“title”)] public string Title { get; set; } } 还有一个名为DbField的扩展方法。 我想知道是否可以在c#中从对象实例获取自定义属性。 Test t = new Test(); string fieldName = t.Title.DbField(); //fieldName will equal “title”, the same name passed into the attribute above 可以这样做吗?

反思和generics类型

我正在为类构造函数编写一些代码,它循环遍历类的所有属性,并调用一个通用的静态方法,该方法使用外部API中的数据填充我的类。 所以我把它作为一个示例类: public class MyClass{ public string Property1 { get; set; } public int Property2 { get; set; } public bool Property3 { get; set; } public static T DoStuff(string name){ // get the data for the property from the external API // or if there’s a problem return ‘default(T)’ } } 现在在我的构造函数中我想要这样的东西: public MyClass(){ […]

我如何在C#中获取调用方法

可能重复: 如何找到调用当前方法的方法? 我需要一种方法来了解C#中调用方法的名称。 例如: private void doSomething() { // I need to know who is calling me? (method1 or method2). // do something pursuant to who is calling you? } private void method1() { doSomething(); } private void method2() { doSomething(); }

在不同的AppDomain中加载/卸载程序集

我需要在运行时加载的程序集中执行一个方法。 现在我想在方法调用后卸载那些已加载的程序集。 我知道我需要一个新的AppDomain,所以我可以卸载库。 但在这里,出现了问题。 要加载的程序集是我的插件框架中的插件。 他们根本没有切入点。 我所知道的是它们包含一些实现给定接口的类型。 旧的非AppDomain代码看起来像这样(稍微缩短): try { string path = Path.GetFullPath(“C:\library.dll”); AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; Assembly asm = Assembly.LoadFrom(path); Type[] types = asm.GetExportedTypes(); foreach (Type t in types) { if ((t.GetInterface(“IStarter”) != null) && !t.IsAbstract) { object tempObj = Activator.CreateInstance(t); MethodInfo info = t.GetMethod(“GetParameters”); if (info != null) { return info.Invoke(tempObj, null) as […]