可以使用反射访问方法变量吗? [英] Can a method variable be accessed using reflection?

查看:51
本文介绍了可以使用反射访问方法变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用反射访问方法中定义的变量?

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

我必须在 Hibernate 中创建条件.数据库是基于属性的.

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 循环条件中定义的局部变量.

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

推荐答案

你不能.如果它是在方法中定义的,它是否是仅在该范围内定义的局部变量.由于该方法没有超出其范围的状态,因此您无权访问.

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.

这篇关于可以使用反射访问方法变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆