In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In order to solve the problem of how to bind data with expressions in C #, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
First of all, the data class still has to write accessors for each attribute, and there is no way to limit the language features. But it's not troublesome to have a template.
Public class A:BindAble {private int _ a; public int a {get = > GetProperty (ref _ a); set = > SetProperty (ref _ a, value);}
Private int _ b; public int b {get = > GetProperty (ref _ b); set = > SetProperty (ref _ b, value);}}
A more reasonable way is to mark attributes with meta tags
[BindAble] public int a; [BindAble] public int b
But if you want to achieve it, the realistic way is to compile and generate code to inject DLL (xLua method). It's not impossible to write, but I'll just leave this as a toy.
When binding data, you need to new a BindHandler, then call BindProperty, and enter the Lambda expressions of setter and getter. Under the language characteristics, you can only write this if you don't want to reflect.
New BindHandler () .BindProperty (v = > obj2.a = v, () = > obj.a)
This code indicates that the value of obj2.a will always be the same as that of obj.a.
Unbinding calls the UnBind method of BindHandler.
If you want to bind to a function
New BindHandler () .BindAction (e = > xxxxxx) .AddTarget (() = > obj.a)
AddTarget can be called multiple times.
If the binding target is a list and you want to be aware of the changes in the elements in the list, you need to switch to the wrapped list under the Collections directory, which is impossible.
Finally, since it is a Lambda expression, the getter side can write multiple parameters, that is:
New BindHandler () .BindProperty (v = > obj3.a = v, () = > obj.a + obj2.a)
In this way, the value of obj3.a will always be equal to the sum of obj.an and obj2.a, and obj3.a will be updated once if either of the latter two values changes.
Two-way binding, limited to the condition can only write two BindHandler, because the weight will not be triggered indefinitely.
New BindHandler () .BindProperty (v = > obj2.a = v, () = > obj.a); new BindHandler () .BindProperty (v = > obj.a = v, () = > obj2.a)
The implementation of the basic binding is obviously to rewrite the setter and then trigger the event, then listen and assign a value to another number, and there is nothing to say. What's interesting about this implementation is that you can automatically bind multiple data within an expression by entering an expression, which requires an AddTarget no matter how you think about it.
The general idea is to use the Expression of C #, parse the Lambda expression to get the properties that need to be bound, and then listen. I wanted to use this at first, but Expression probably got called back by IOS for involving JIT and hesitated for a long time. But without this feature, the binding will not be able to bypass the attribute name in the form of an input string, and it will fall into hidden dangers such as "spelling mistakes" and "refactoring failure", which will lose its use value to me.
Fortunately, another way to achieve it was found later. This method is very coquettish, if you look at my code directly, you may not be able to react. Explain it specifically.
The code I'm listening to in BindHandler looks like this:
Internal static BindHandler recordingBindHandler = null;void AddExpressionListener (Func expression) {recordingBindHandler = this; expression.Invoke (); recordingBindHandler = null;}
Simply put the Handler on a static field for a while, and then call the expression of the binding target.
Looks like you didn't do anything? But when you call expression, it actually triggers the getter of the properties involved in it.
And the getter for those attributes looks like this:
Protected T GetProperty (ref T property, [CallerMemberName] string propertyName = null) {if (BindHandler.recordingBindHandler! = null) {BindHandler.recordingBindHandler.AddTarget (this, propertyName);} return property;}
The requirement is realized through such a reverse registration.
Of course, this is certainly different from using Expression directly, for example, as long as there is a & & a kind of operator that will cause an interrupt, the latter part may not be called and not registered. So you have to be careful when using it.
In most cases, it should be fine.
This thing looks very advanced, but I am surprised that the amount of code required for implementation is very small. When implementing the subsequent features, I found that it was only possible to add a few lines. I thought it was impossible to do so, but I found that I was right after running the test.
So it was fun anyway.
In fact, you can also consider making:
New BindHandler () .BindProperty (() = > obj2.a = obj.a)
It's more intuitive, but I'm afraid someone will write like this.
New BindHandler () .BindProperty (() = > p.obj2.a = obj.a); this is the answer to the question about how to bind data with expressions in C #. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.