我要做的是使用字符串在类中设置属性的值.例如,我的类具有以下属性:
myClass.Name
myClass.Address
myClass.PhoneNumber
myClass.FaxNumber
Run Code Online (Sandbox Code Playgroud)
所有字段都是字符串类型,所以我提前知道它总是一个字符串.现在我希望能够使用字符串设置属性,就像使用DataSet对象一样.像这样的东西:
myClass["Name"] = "John"
myClass["Address"] = "1112 River St., Boulder, CO"
Run Code Online (Sandbox Code Playgroud)
理想情况下,我只想分配一个变量,然后使用变量中的字符串名称设置属性
string propName = "Name"
myClass[propName] = "John"
Run Code Online (Sandbox Code Playgroud)
我正在阅读关于反思的内容,也许是这样做的方法,但我不确定如何设置它,同时在课堂上保持属性访问不变.我想仍然可以使用
myClass.Name = "John"
Run Code Online (Sandbox Code Playgroud)
任何代码示例都非常棒.
我有一个java.lang.reflect.Method对象,我想知道它是否是返回类型void.
我检查了Javadocs,并且有一个getReturnType()返回Class对象的方法.问题是,如果方法无效,他们不会说返回类型是什么.
谢谢!
假设我有一个动态变量:
dynamic d = *something*
Run Code Online (Sandbox Code Playgroud)
现在,有些东西d从字符串数组中创建了我拥有的属性:
string[] strarray = { 'property1','property2',..... }
Run Code Online (Sandbox Code Playgroud)
我事先不知道房产名称.
如何在代码d中创建一次并从数据库中提取strarray,我可以获取值吗?
我想得到d.property1 , d.property2.
我看到该对象有一个_dictionary包含键和值的内部字典,如何检索它们?
我java.lang.NoSuchFieldException试图运行以下方法时得到一个:
public void getTimes(String specialty, String day) {
ArrayList<Tutor> withSpec = new ArrayList<Tutor>();
for (Tutor t : tutorList){
try {
Time startTime = (Time)t.getClass().getField(day + "Start").get(t);
} catch (NoSuchFieldException | SecurityException | IllegalAccessException ex) Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex); }
Run Code Online (Sandbox Code Playgroud)
错误就行了 Time startTime = (Time)t.getClass().getField(day + "Start").get(t);
我不明白这个错误,因为monStart是Tutor该类的一个字段:
Public class Tutor implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "tutorID")
private Integer tutorID;
....
@Column(name = "monStart")
@Temporal(TemporalType.TIME)
Date …Run Code Online (Sandbox Code Playgroud) 请看下面的代码:
Method methodInfo = MyClass.class.getMethod("myMethod");
Run Code Online (Sandbox Code Playgroud)
这可行,但方法名称作为字符串传递,因此即使myMethod不存在,这也将编译.
另一方面,Java 8引入了方法引用功能.它在编译时检查.可以使用此功能获取方法信息吗?
printMethodName(MyClass::myMethod);
Run Code Online (Sandbox Code Playgroud)
完整示例:
@FunctionalInterface
private interface Action {
void invoke();
}
private static class MyClass {
public static void myMethod() {
}
}
private static void printMethodName(Action action) {
}
public static void main(String[] args) throws NoSuchMethodException {
// This works, but method name is passed as a string, so this will compile
// even if myMethod does not exist
Method methodInfo = MyClass.class.getMethod("myMethod");
// Here we pass reference to a method. It …Run Code Online (Sandbox Code Playgroud) 我有这个代码:
Type typeOfObjectsList = new TypeToken<ArrayList<myClass>>() {}.getType();
List<myClass> objectsList = new Gson().fromJson(json, typeOfObjectsList);
Run Code Online (Sandbox Code Playgroud)
它将JSON字符串转换List为对象.但是现在我希望在运行时定义ArrayList动态类型(不仅仅是myClass).
所述ArrayList的项目类型将与被定义反射.
我试过这个:
private <T> Type setModelAndGetCorrespondingList2(Class<T> type) {
Type typeOfObjectsListNew = new TypeToken<ArrayList<T>>() {}.getType();
return typeOfObjectsListNew;
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.这是例外:
java.sql.SQLException: Fail to convert to internal representation: {....my json....}
Run Code Online (Sandbox Code Playgroud) Class someInterface = Class.fromName("some.package.SomeInterface");
Run Code Online (Sandbox Code Playgroud)
我现在如何创建一个实现的新类someInterface?
我需要创建一个新类,并将其传递给需要SomeInterface作为参数的函数.
这是关于java私有构造函数的这个问题的后续内容.
假设我有以下课程:
class Foo<T>
{
private T arg;
private Foo(T t) {
// private!
this.arg = t;
}
@Override
public String toString() {
return "My argument is: " + arg;
}
}
Run Code Online (Sandbox Code Playgroud)
我如何构建new Foo("hello")使用反射?
回答
根据jtahlborn的回答,以下作品:
public class Example {
public static void main(final String[] args) throws Exception {
Constructor<Foo> constructor;
constructor = Foo.class.getDeclaredConstructor(Object.class);
constructor.setAccessible(true);
Foo<String> foo = constructor.newInstance("arg1");
System.out.println(foo);
}
}
Run Code Online (Sandbox Code Playgroud) 有没有办法在.NET(2.0)中使用反射调用重载方法.我有一个动态实例化从公共基类派生的类的应用程序.出于兼容性目的,此基类包含2个同名方法,一个包含参数,另一个不包含.我需要通过Invoke方法调用无参数方法.现在,我得到的只是一个错误告诉我,我正试图调用一个模棱两可的方法.
是的,我可以将对象转换为我的基类的实例并调用我需要的方法.最终会发生,但现在,内部并发症将无法实现.
任何帮助都会很棒!谢谢.
在给定的命名空间下,我有一组实现接口的类.我们称之为ISomething.我有另一个类(让我们称它CClass),它知道ISomething但不知道实现该接口的类.
我希望CClass找到所有的实现ISomething,实例化它的实例并执行该方法.
有没有人知道如何用C#3.5做到这一点?
reflection ×10
java ×6
c# ×3
.net ×1
.net-2.0 ×1
.net-4.0 ×1
arraylist ×1
constructor ×1
gson ×1
interface ×1
invoke ×1
java-8 ×1
lambda ×1
methods ×1
overloading ×1
private ×1
properties ×1