Tag: reflection

C# – 将.NET程序集加载到单独的AppDomain中,以便卸载它

可能重复: 将DLL加载到单独的AppDomain中 将.NET程序集加载到单独的AppDomain中的正确方法是什么,这样您就可以访问其类型/类,但仍然可以卸载它(并重新加载它)。 这是前面讨论的切线: C# – 正确加载程序集,查找类和调用Run()方法

从派生自基类的程序集中获取所有类型

我试图检查程序集的内容,并找到其中直接或间接派生自Windows.Forms.UserControl的所有类。 我这样做: Assembly dll = Assembly.LoadFrom(filename); var types = dll.GetTypes().Where(x => x.BaseType == typeof(UserControl)); 但是它给出了一个空列表,因为没有类直接扩展UserControl。 我不太了解reflection快速完成它,如果我不需要,我宁愿不写一个递归函数。

如何从Action 获取方法的自定义属性?

如何从Action委托中获取方法的自定义属性? 例: //simple custom attribute public class StatusAttribute : Attribute { public string Message { get; set; } = string.Empty; } // an extension methodto wrap MethodInfo.GetCustomAttributes(Type, Bool) with // generics for the custom Attribute type public static class MethodInfoExtentions { public static IEnumerable GetCustomAttributes(this MethodInfo methodInfo, bool inherit) where TAttribute : Attribute { object[] […]

当类使用generics和generics类型参数时,如何获取正确的MethodInfo对象

我想知道是否有人能够演示如何使用Type的GetMethod()方法来检索以下签名的MethodInfo对象: Class.StaticMethod(T arg1, IInterface1 arg2, IEnumerable arg3) 谢谢, XAM

属性/方法内联和对reflection的影响

关于SO的问题之一的回答是由Valentin Kuzub评论的,他认为通过JIT编译器内联属性将导致reflection停止工作。 案件如下: class Foo { public string Bar { get; set; } public void Fuzz(Expression<Func> lambda) { } } Fuzz(x => x.Bar); Fuzz函数接受lambda表达式并使用reflection来查找属性。 这是HtmlHelper扩展中MVC的常见做法。 即使Bar属性被内联,我也不认为reflection将停止工作,因为它是对Bar的调用,内联和typeof(Foo).GetProperty(“Bar”)仍将返回有效的PropertyInfo 。 你能否证实这一点,或者我对方法内联的理解是错误的?

如何获取属于自定义属性的属性?

我需要在自定义属性中找到应用自定义属性的属性的类型。 例如: [MyAttribute] string MyProperty{get;set;} 给定MyAttribute的实例,我如何获得MyProperty的Type描述符? 换句话说,我正在寻找System.Type.GetCustomAttributes()的反面

迭代变量和查找特定类型的实例的技术

我想在我的进程中迭代内存中的变量(通过动态加载),并查找特定类型的实例。 以前我可以找到特定类型(或内存中的所有类型)。 我可以创建类型的实例,我可以获得包含在不同类型的字段中的实例,但我不知道只是为了“搜索”特定类型的实例。

如何使用reflection来查找实现特定接口的属性?

考虑这个例子: public interface IAnimal { } public class Cat: IAnimal { } public class DoStuff { private Object catList = new List(); public void Go() { // I want to do this, but using reflection instead: if (catList is IEnumerable) MessageBox.Show(“animal list found”); // now to try and do the above using reflection… PropertyInfo[] properties […]

无法将’System.Object ‘类型的对象强制转换为’MyObject ‘,是什么给出的?

场景: 我正在编写一个层来将3个类似的Web服务抽象为一个可用的类。 每个Web服务都公开一组共享共性的对象。 我创建了一组利用共性的中间对象。 但是在我的图层中,我需要在Web服务对象和我的对象之间进行转换。 在调用Web服务之前,我已经使用reflection在运行时创建了相应的类型,如下所示: public static object[] CreateProperties(Type type, IProperty[] properties) { //Empty so return null if (properties==null || properties.Length == 0) return null; //Check the type is allowed CheckPropertyTypes(“CreateProperties(Type,IProperty[])”,type); //Convert the array of intermediary IProperty objects into // the passed service type eg Service1.Property object[] result = new object[properties.Length]; for (int i […]

调用generics类的方法

这是上下文: 我尝试编写一个映射器来动态地将我的DomainModel对象转换为ViewModel Ojects。 我得到的问题是,当我尝试通过reflection调用generics类的方法时,我得到了这个错误: System.InvalidOperationException:无法对ContainsGenericParameters为true的类型或方法执行后期绑定操作。 有人可以帮助我找出错误在哪里吗? 这将不胜感激 这是代码(我试图简化它): public class MapClass { public string Test() { return test } public void MapClassReflection(SourceType source, ref DestinationType destination) { Type sourceType = source.GetType(); Type destinationType = destination.GetType(); foreach (PropertyInfo sourceProperty in sourceType.GetProperties()) { string destinationPropertyName = LookupForPropertyInDestinationType(sourceProperty.Name, destinationType); if (destinationPropertyName != null) { PropertyInfo destinationProperty = destinationType.GetProperty(destinationPropertyName); […]