开发者

Variable Binding Class

I believe this is a tough one...

I'm trying to create a collection of variable binding in a class.

It is intended to look something like this:

Dim x as integer, s as string
Dim c as new VBindClass

x = 1
s = "Hello"

c.Add x, s
Debug.Print c.value(x) '= 1
Debug.Print c.value(开发者_开发技巧s) '= "Hello"

Is there some function that allow us to retrieve a unique ID for a given variable and also get/set based on variable?

Update:

At last, I've managed to found the answer. Here's the code:

Dim gh As Runtime.InteropServices.GCHandle = Runtime.InteropServices.GCHandle.Alloc(obj)
Return Runtime.InteropServices.GCHandle.ToIntPtr(gh).ToInt64


You cannot bind directly to method variables, since a: there is no reflection to them, and b: they may not actually exist by the time the compiler is through. Also, in implementation terms it would be very confusing for something like a class (VBindClass) to have access to the current stack position (since the two would be miles apart).

You can reflect on fields though (variables on a class / struct) using reflection (Type.GetFields(...), usually needing the non-public | instance binding-flags).

For your example, "captured variables" might be of use, but you'd need to be using lambdas / expressions for that to work. But in C# terms:

int x = 1;
Action a = () => Debug.WriteLine(x);

// can now hand "a" anywhere, including to other classes, into events etc

x = 27;
//now invoke a **from anywhere** - it'll show the current value (27)
a();


IF I understand correctly what you are looking for you might want to have a look at using

Dictionary Class

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜