Tag: reflection

Javareflection – setAccessible的影响(true)

我正在使用一些注释来dynamic设置类中字段的值。 因为我想这样做,无论它是公共的,保护的还是私人的,我每次在调用set()方法之前都要在Field对象上调用setAccessible(true) 。 我的问题是setAccessible()调用对字段本身有什么样的影响? 更具体地说,这是一个私人领域,这组代码调用setAccessible(true) 。 如果代码中的其他地方然后通过reflection来检索相同的字段,该字段是否可以被访问? 或者getDeclaredFields()和getDeclaredField()方法每次都返回一个Field对象的新实例? 我想另外一个说明这个问题的方法是,如果我调用setAccessible(true) ,在完成之后将它设置回原始值有多重要?

使用C#reflection调用构造函数

我有以下情况: class Addition{ public Addition(int a){ a=5; } public static int add(int a,int b) {return a+b; } } 我打电话给另一个classjoin: string s="add"; typeof(Addition).GetMethod(s).Invoke(null, new object[] {10,12}) //this returns 22 我需要一种类似于上述reflection语句的方法来创build一个使用Addition(int a)types的新对象, 所以我有strings= "Addition" ,我想创build一个新的对象使用reflection。 这可能吗?

如何获得SQLite数据库表中列的列表?

我正在寻找检索表中列的列表。 数据库是最新版本的SQLite(3.6,我相信)。 我正在寻找与SQL查询做到这一点的代码。 与列相关的元数据的额外奖励点数(例如长度,数据types等)

Python:在运行时改变方法和属性

我希望创build一个Python类,我可以添加和删除属性和方法。 我怎样才能做到这一点? 哦,请不要问为什么。

用reflection“铸造”

考虑下面的示例代码: class SampleClass { public long SomeProperty { get; set; } } public void SetValue(SampleClass instance, decimal value) { // value is of type decimal, but is in reality a natural number => cast instance.SomeProperty = (long)value; } 现在我需要通过反思做类似的事情: void SetValue(PropertyInfo info, object instance, object value) { // throws System.ArgumentException: Decimal can not be converted […]

获取一个类的实例方法的列表

我有一个class级: class TestClass def method1 end def method2 end def method3 end end 我怎样才能得到这个类中的方法列表( method1 , method2 , method3 )?

避免模​​糊匹配exception

我正在调用一个静态方法通过reflection的typesparsing ,因为我不知道在编译时的对象的types(我知道,但是,它有一个Parse方法,采取一个string)。 然而,我得到一个模糊的匹配exception,大概是因为有很多重载Parse方法每个都采取单个对象(string,整数,双等)。 我怎样才能在我的方法调用更具体,以确保我到达正确的方法( parsing(strings) )和exception不会抛出。 我的代码如下所示: Type returnType = p.PropertyType; object value = returnType.GetMethod("Parse").Invoke(null, new string[] { "1" });

在Java中是否有像instanceOf(Class <?> c)?

我想检查一个对象o是类C还是C的一个子类的一个实例。 例如,如果p是类Point我希望x.instanceOf(Point.class)为true ,并且x.instanceOf(Object.class)为true 。 我希望它也适用于盒装原始types。 例如,如果x是一个Integer那么x.instanceOf(Integer.class)应该是true 。 有这样的事吗? 如果没有,我该如何实施这样的方法?

使用PropertyInfo来查找属性types

我想dynamic地parsing一个对象树来做一些自定义validation。 validation并不重要,但我想更好地理解PropertyInfo类。 我会做这样的事情, public bool ValidateData(object data) { foreach (PropertyInfo propertyInfo in data.GetType().GetProperties()) { if (the property is a string) { string value = propertyInfo.GetValue(data, null); if value is not OK { return false; } } } return true; } 真的,我现在唯一关心的部分是'如果财产是一个string'。 我如何从PropertyInfo对象中找出它是什么types的。 我将不得不处理诸如string,整数,双打等基本的东西。 但是我也必须处理对象,如果需要的话,我需要遍历这些对象内部的对象树来validation其中的基本数据,它们也会有string等。 谢谢。

如何查找inheritance自特定types的程序集中的所有typesC#

你如何得到从一个特定的其他typesinheritance的所有types的集合?