询问者
如何取得在当前范围内声明的变量?

问题
-
全部回复
-
直接在类内部公开一个方法即可:
public class TestClass
{
public void ShowVariable(Type type)
{
Console.WriteLine(type);
}}
外部调用这个类的这个方法即可。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
你好,你可以将你要的 变量 宣告在 class。
例如:这样只要在同一个 class 就可取得该 变量了。
class xxx
{
A a1=new A();
B b1=new B();
A a2=new A();
B a2=new B();
static void main()
{
using(TestClass testclass =new TestClass())
{
testClass.ShowVariable(typeof(A));
}
}
}
这并非我本意,我希望达到TransactionScope的效果,testclass需要对在其作用域内声明的变量进行某些操作。
-
直接在类内部公开一个方法即可:
public class TestClass
{
public void ShowVariable(Type type)
{
Console.WriteLine(type);
}}
外部调用这个类的这个方法即可。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & CatstestClass.ShowVariable的作用是对在testClass作用域内的声明的变量进行过滤,并输出类型为type的变量的名称。
这可能需要获取当前的堆栈信息,但我找不到相关方法或者API。
-
恐怕需要反射了。
下面给你一个例子,遍历指定某个类的私有变量,并且检测这些变量是否可以由类自身转化:
namespace Csharp { public class A { private string s = null; private object obj = null; private A a = null; } public class MainTest { static void ShowVariable(Type tp) { foreach (var item in tp.GetFields(BindingFlags.NonPublic|BindingFlags.Instance)) { if (item.FieldType.IsAssignableFrom(tp)) { Console.WriteLine(item.FieldType); } } } static void Main(string[] args) { ShowVariable(typeof(A)); } } }
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
恐怕需要反射了。
下面给你一个例子,遍历指定某个类的私有变量,并且检测这些变量是否可以由类自身转化:
namespace Csharp { public class A { private string s = null; private object obj = null; private A a = null; } public class MainTest { static void ShowVariable(Type tp) { foreach (var item in tp.GetFields(BindingFlags.NonPublic|BindingFlags.Instance)) { if (item.FieldType.IsAssignableFrom(tp)) { Console.WriteLine(item.FieldType); } } } static void Main(string[] args) { ShowVariable(typeof(A)); } } }
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats非常感谢,但是反射是无法达到我所需要的效果的。我希望达到类似于TransactionScope的效果,testclass需要对在其作用域内声明的变量进行某些操作。
.Net中的System.Diagnostics.StackTrace可以跟踪到当前的函数名,但却无法跟踪到变量,我想找到类似的可以跟踪到变量的方法。
-
你想获取在Using块中的变量?
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
你想获取在Using块中的变量?
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
是的。