Tag: reflection

只获取直接界面而不是全部?

我有一个类似下面的课程。 GetInterfaces()说 如果当前Type表示generics类型或generics方法的定义中的类型参数,则此方法搜索接口约束以及从类或接口约束inheritance的任何接口。 我有可能没有得到任何inheritance的接口吗? 当我在ABC上使用GetInterfaces时我只想看DEF,而不是DEF和GHI。 interface DEF : GHI {…} class ABC : DEF {…}

动态设置generics类型参数

继我的问题之后,我正在尝试创建一个通用的值相等比较器。 我以前从未玩过reflection,所以不确定我是否在正确的轨道上,但无论如何我到目前为止都有这个想法: bool ContainSameValues(T t1, T t2) { if (t1 is ValueType || t1 is string) { return t1.Equals(t2); } else { IEnumerable properties = t1.GetType().GetProperties().Where(p => p.CanRead); foreach (var property in properties) { var p1 = property.GetValue(t1, null); var p2 = property.GetValue(t2, null); if( !ContainSameValues(p1, p2) ) return false; } } return true; } […]

获得代表信息的反思

通过执行以下命令,我可以获得有关方法的信息 Type t=typeof(someType); MemberInfo[] mInfo = t.GetMethods(); 如何获取有关在类型中声明的委托的信息?

C#中两个对象之间的差异

我想知道如何找到同一类的两个对象之间的区别。 因此,如果我有一个Person类,唯一的区别是Age,它将返回不同的字段/字段。 谢谢

我可以使用reflection来查找ASP.NET中的bin / 文件夹而不是asp临时文件夹

我有一个ASP.NET网站,我想找到/ bin / [Configuration]文件夹使用外部工具(exe文件)。 当我使用reflection来获取调用程序集位置时,它返回类似于: C:\Windows\Microsoft.NET\Framework\\…\Temporary ASP.NET Files\a1388a5e\\…\my.dll 由于每个dll在临时ASP.NET文件下都有自己的目录,因此对我来说失败了。 如何获取已编译的二进制文件夹的位置,其中dll和.exe是(即bin /)而不是asp.net的临时缓存? 笔记 此代码位于支持库中,可以从ASP.NET网站或其他控制台/ Windows应用程序调用。

如何将程序集加载到内存中并执行它

这就是我在做的事情: byte[] bytes = File.ReadAllBytes(@Application.StartupPath+”/UpdateMainProgaramApp.exe”); Assembly assembly = Assembly.Load(bytes); // load the assemly //Assembly assembly = Assembly.LoadFrom(AssemblyName); // Walk through each type in the assembly looking for our class MethodInfo method = assembly.EntryPoint; if (method != null) { // create an istance of the Startup form Main method object o = assembly.CreateInstance(method.Name); // invoke the […]

如何使用reflection将事件处理程序附加到事件?

我知道EventInfo.AddEventHandler(…)方法,它可以用来将处理程序附加到事件。 但是,如果我甚至无法定义事件处理程序的正确签名,应该怎么做,因为我甚至没有引用处理程序所期望的事件args? 我将用正确的代码解释问题。 //当我在我的解决方案中提供一切可用的场景时,Zero Reflection Scenario。 internal class SendCommentsManager { public void Customize(IRFQWindowManager rfqWindowManager) { rfqWindowManager.SendComment += HandleRfqSendComment; } private void HandleRfqSendComment(object sender, SendCommentEventArgs args) { args.Cancel = true; } } 现在,我希望通过使用reflection来实现相同的目标。 我已经能够弄清楚它的大部分但是当我将一个委托附加到事件时(使用AddEventHandler )它会抛出”Error binding to target method.” 例外。 我理解这个exception背后的原因,将错误的委托附加到事件中。 但必须有一些方法来实现这一目标。 internal class SendCommentsManagerUsingReflection { public void Customize(IRFQWindowManager rfqWindowManager) { EventInfo eventInfo = rfqWindowManager.GetType().GetEvent(“SendComment”); eventInfo.AddEventHandler(rfqWindowManager, […]

使用.NET / C中的委托加速Reflection API

if you need to call the method multiple times, use reflection once to find it, then assign it to a delegate, and then call the delegate.此post有注释if you need to call the method multiple times, use reflection once to find it, then assign it to a delegate, and then call the delegate. 。 该delegate如何以及为何更快地运作? 谁能有一些例子吗? 我可以称之为caching吗? […]

没有XmlInclude的序列化

我正在使用.NET序列化反序列化一个名为Method的类。 Method包含实现IAction的对象列表。 我最初使用[XmlInclude]属性来指定实现IAction所有类。 但是现在,我想改变我的程序来加载目录中的所有dll并删除实现IAction的类。 然后,用户可以反序列化包含实现IAction的操作的文件。 我不再控制实现IAction的类,因此我不能使用[XmlInclude] 。 有没有办法在运行时设置此属性? 或者为实现类设置了类似的属性? public class Method { public List Actions = new List(); } public interface IAction { void DoExecute(); } public static Type[] LoadActionPlugins(string pluginDirectoryPath) { List pluginTypes = new List(); string[] filesInDirectory = Directory.GetFiles(pluginDirectoryPath, “*.dll”, SearchOption.TopDirectoryOnly); foreach (string pluginPath in filesInDirectory) { System.Reflection.Assembly actionPlugin = System.Reflection.Assembly.LoadFrom(pluginPath); Type[] […]

如何使用reflection调用通用异步方法

public interface IBar { } public class Bar : IBar { } public class Bar2 : IBar { } public interface IFoo { Task Get(T o) where T : IBar; } public class Foo : IFoo { public async Task Get(T o) where T : IBar { … } } 然后我可以使用reflection调用此方法: var method = typeof(IFoo).GetMethod(nameof(IFoo.Get)); […]