Can a method variable be accessed using reflection?

ⅰ亾dé卋堺 提交于 2021-01-28 01:50:42

问题


How can I access a variable defined inside a method by using reflection?

I have to create criteria in Hibernate. The database is attribute based.

Consider a movie. A movie can have many languages and many genres. e.g.

Movieid           Property           Value
1                 Language           Hindi
1                 Language           English
1                 genre              action
2                 genre              comedy

Let's assume there are a thousand records of different movies. The user selects a set of genres(action) and languages(hindi, english) to filter the result. Now, I have to create criteria in this order : expression = (property= english and genre = action) or (property= hindi and genre = action)

For the implementation, I have to lookup into a local variable defined inside a for loop condition.


回答1:


You can't. If it's defined in a method, is it a local variable defined in that scope only. Since the method had no state outside of its scope, you have nothing to access.

Accessing a class member (which, by definition, is a state) you can access via "regular" reflection.




回答2:


I'm guessing that your data for these movies are stored in an RDBMS. Just do a simple relational query to get exactly the results you need. This problem is a perfect example of the sort of for which people developed relational database management systems.



来源:https://stackoverflow.com/questions/910155/can-a-method-variable-be-accessed-using-reflection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!