Tag: reflection

在C#中获取命名空间中的类列表

我需要以编程方式获取给定命名空间中所有类的List 。 我怎样才能在C#中实现这个(reflection?)?

从应用程序的资源加载.NET程序集并从内存运行它,但不终止主/主机应用程序

介绍 我正在使用David Heffernan共享的下一个C#代码示例, 从应用程序的资源加载.NET程序集并从内存运行它 : Assembly a = Assembly.Load(bytes); MethodInfo method = a.EntryPoint; if (method != null) method.Invoke(a.CreateInstance(method.Name), null); 在这里,我只是在VB.NET中分享我正在使用的改编: Public Shared Sub Execute(ByVal resource As Byte(), ByVal parameters As Object()) Dim ass As Assembly = Assembly.Load(resource) Dim method As MethodInfo = ass.EntryPoint If (method IsNot Nothing) Then Dim instance As Object = ass.CreateInstance(method.Name) method.Invoke(instance, […]

Assembly.ReflectionOnlyLoadFrom不工作

我有一个Assembly Library1.dll ,它包含一些接口,这些接口被序列化为数据库中的字节数组。 出于某些原因,我们必须更改接口属性和定义。 所以现在我正在编写一个迁移实用程序。 所以我有2个版本的Library1.dll ,在我的实用程序中我创建了一个文件夹,我存储了新版本的Library1.dll 。 此实用程序反过来也引用Library1.dll因此bin文件夹中包含Library1.dll但此dll是在旧版本上编译的。 我的新版本的Library1.dll存储在一个私有路径中,我将传递给Assembly.ReflectionOnlyLoadFrom函数来实例化,因此加载了程序集上的GetTypes ,这将使我能够进行数据转换。 但是当我尝试从私有路径加载Library1.dll时,我总是得到ReflectionTypeLoadException 。 请帮帮忙!!! 任何帮助,将不胜感激。 我真的被卡住了。 谢谢,AG

SetValue在c#中的reflection

考虑以下代码: var future = new Future(); future.GetType().GetProperty(info.Name).SetValue(future, converted); 在上面的代码中,我们应该为SetValue传递两个参数。 首先,我们要设置其属性的对象。 第二,新的价值。 但我们选择了具体的财产。 为什么我们应该传递第一个参数来设置值,因为我们之前设置了未来的对象!?

如何在运行时获取当前的类名?

我正在尝试将当前的类名称转换为字符串。 例如: public class Marker : Mark { string currentclass = ???; } public abstract class MiniMarker : Mark { } 我想从Marker类中获取字符串,所以我不必将它放在我从中创建的每个抽象类中。 我希望字符串是MiniMarker ,或者是抽象类的名称。 我尝试了MethodBase.GetCurrentMethod().DeclaringType ,但它没有用。

使用reflection和c#调用静态方法时出现问题

我有这两个class: Item : BusinessBase where T : Item { public static T NewItem() { //some code here } } Video : Item { } 现在我想使用reflection在类Video上调用NewItem()方法。 当我尝试这个: MethodInfo inf = typeof(Video).GetMethod(“NewItem”, BindingFlags.Static); 执行此行后的对象inf仍为null。 我可以在类Video上调用静态NewItem()方法吗?

GetValue,GetConstantValue和GetRawConstantValue之间的区别

PropertyInfo类上的GetValue , GetConstantValue和GetRawConstantValue方法有什么区别? 遗憾的是,MSDN文档在这个问题上并不十分清楚。

如何使用reflection来获取generics类型的扩展方法

从各种来源的teh interwebs我收集了以下function: public static Nullable TryParseNullable(this Nullable t, string input) where T : struct { if (string.IsNullOrEmpty(input)) return default(T); Nullable result = new Nullable(); try { IConvertible convertibleString = (IConvertible)input; result = new Nullable((T)convertibleString.ToType(typeof(T), CultureInfo.CurrentCulture)); } catch (InvalidCastException) { } catch (FormatException) { } return result; } 我把它变成了一个扩展方法,如果我直接调用它就可以正常工作: int? input = new int?().TryParseNullable(“12345”); 当我尝试使用另一个generics函数的上下文中的reflection来调用它时,我的问题就出现了。 SO充满了描述如何获得generics方法和静态方法的MethodInfo的答案,但我似乎无法以正确的方式将它们组合在一起。 […]

获取匿名类型的读/写属性

我需要获取可以写入的匿名类型的所有属性。 例如: var person = new {Name = “Person’s Name”, Age = 25}; Type anonymousType = person.GetType(); var properties = anonymousType.GetProperties(BindingFlags.Public | BindingFlags.Instance); 问题是所有属性的CanWrite属性都为false 。 对于非匿名类型,返回true。 我也试过调用PropertyInfo.GetSetMethod() ,它返回null 。 如何检查属性是否可以写入? 编辑:也许知道一个类型是否是匿名的就足够了。 如何使用reflection查明某个类型是匿名的?

使用Reflection调用属性的方法

我要做的是使用Reflection调用属性的方法。 我有原始的Control(一个ComboBox),属性的PropertyInfo(ComboBox.Items)和方法的名称(ComboBox.Items.Add)。 我已经尝试了下面的代码来获取,更改,设置但它不起作用,因为Items没有setter。 PropertyInfo p = controlType.GetProperty(propertyName); // gets the property (‘Items’) MethodInfo m = p.PropertyType.GetMethod(methodName); // gets the method (‘Items.Add’) object o = p.GetValue(newControl, null); // gets the current ‘Items’ m.Invoke(o, new object[] { newValue }); // invokes ‘Add’ which works p.SetValue(newControl, o, null); // exception: ‘Items’ has no setter 有人有建议吗? 谢谢