ArticleDirectory
- What is index property?
- Why do we need it?
- Why does C # Not support defining index attributes?
- Index attribute implementation
- Use of index attributes
- Summary
First of all, I wish you a happy New Year (a friend wishes me a happy New Year, but I always feel that I don't have that taste. Happy New Year always means a happy Spring Festival, but happy new year is acceptable ~~). In this article, I will introduce a new feature about C #4.0, index attribute, which is rarely mentioned by C # team. This article mainly introduces the index attribute, why it is needed, and why C # does not fully support it. At present, the implementation of the C # index attribute and how to use it.
What is index property?
The index attribute refers to the attribute that can return the specified result based on the index value. It is an atomic operation of the access attribute, such as someobject. someproperty [Index]. The index attribute is similar to a common attribute. The only difference is that it has an additional parameter that represents the index value. This new feature was added to C #4.0 later than in vs 2010 beta1. It is a new feature added in beta2.
Why do we need it?
If you often deal with COM, You should have encounteredCode(This code is also an example written by Scott hanselman at beta1 release :):
VaR Excel = new excel. application (); excel. visible = true; excel. workbooks. add (); excel. get_range ("A1 "). value2 = "process name"; excel. get_range ("B1 "). value2 = "memory usage ";
This code is only part of it. In fact, you may encounter more difficult code. E.g.
Myobject. set_indexedproperty (myobject. get_indexedproperty (INDEX) + 1 ). It looks really bloated. Why can't we call it like this? Myobject. indexedproperty [Index] ++.
As a long-standing module, COM has many attribute operation methods like above, which also makesProgramThe code written by the employee to interact with COM looks bloated. To solve this problem, C #4.0 puts forward a lot of ways to improve the interaction between C # And com,
Dynamic is one of them, and here C # team uses the index attribute to improve the above mentioned situation, which makes the code written by programmers more concise.
Why does C # Not support defining index attributes?
However, although the index attribute can simplify the code, C # currently has very limited support for it. It can only be used for C # To call objects in COM, you cannot declare or call index attributes in C. In addition, its implementation is not implemented in the compiler or CLR, but simply through the syntax sugar. Why?
The answer given by C # team is the separation of different responsibilities of objects in C #. The reasons are as follows:
An attribute is used to obtain an object. It belongs to its parent object.
The index value is used to enumerate objects returned by an attribute. It should belong to the returned object rather than the parent object of the attribute.
For this reason, C # has never intended to implement this feature in C #4.0. This new feature is not mentioned in C #4.0 language specification or C # Future page, it looks more like a temporary compromise between C # team and COM. In fact, according to the idea of C # team, they have already provided the correct way to use the index attribute. If the reader looks at the sample folder under the vs 2008 directory, you can see that there is a special indexedproperty example. In the sample in 2010 beta2, it becomes indexers_2, but the code is the same. In addition, this article also provides an example on msdn. I will not detail the implementation here, but give a similar example. First, we define a collection class e.g.
Public somecollection {public int this [int Index] {get {// bla} set {// bla} public class indexertest {public somecollection somepropertys {Get; set ;} public int [] otherpropertys {Get; Set ;}}
Then you can use:
Class program {static void main () {indextest it = new indextest (); int A = it. somepropertys [0]; int B = it. otherpropertys [0];}
From the Code, it is not difficult to see C # team's opinion on the index attribute. They think that the role of the index should belong to the object returned by the attribute, rather than the parent object of the attribute, the parent object cannot control the attribute return value through the index value. The index you add to an attribute is actually a set or array operation, rather than being controlled by indextest. From this point of view, we can easily understand why C #4.0 only supports the index attribute of COM.
Index attribute implementation
Currently, C #4.0 does not support the declaration of index attributes. It is only used in COM. When a user references a COM object, the compiler will refer to the com type (the type marked with the tdimport flag) import all the members that look like index attributes and then provide them to users. In this way, you can access these members through the attribute indexer syntax. Next, let's take a look at the specific process based on the example above.
When the compiler encounters an index attribute, it is bound. the object on the left is it, and then it obtains the IT object type indextest. Then, it searches for all the members named somepropertys in the indextest class.
For backward compatibility, the compiler gives priority to attribute name matching, rather than considering whether the returned value of this attribute actually supports the index attribute. From this perspective, index attributes are a bit like extension methods. They are used only when normal method matching fails. For example, if the compiler finds a common property named somepropertys in indextest, it still thinks that this property is what the user wants to call, even if somepropertys does not implement the indexer or is not an array. Then, when the compiler finds that the returned value of this attribute does not support the indexer, the compiler reports an error when the matching fails.
When the compiler cannot find a normal attribute that is the same as the name to be searched, it will check whether indextest is of the COM type. If yes, it will find matching members in all matching index attributes in a similar way as the overload function. In this case, it looks for the GET _ attribute name (INDEX) format, if it can be found, the compiler calls the matching method as an indexed attribute. E.g. The index attribute in the above example is actually the Il code similar to indextest. get_somepropertys (index ).
Use of index attributes
In the end, the index attribute is just a syntactic sugar. The compiler and CLR have not changed much compared with the previous one. However, it can simplify the writing of our code. All the methods you used to call get_x () or set_x () when interacting with COM can now use X [], e.g.
// C # 3.0excel.get _ range ("A1 "). set_value (type. missing, "ID"); // C # 4.0excel.range ["A1"]. value = "ID ";
As mentioned above, Scott hanselman wrote an example when beta1 was released. After beta2, we can make it easier:
VaR Excel = new excel. application (); excel. visible = true; excel. workbooks. add (); excel. range ["A1"]. value = "process name"; excel. range ["B1"]. value = "memory usage ";
Is the code much simpler? Although the above Code is essentially the same, it does look more concise. If you need to deal with COM frequently, I believe you should like index attributes very much. Currently, index attributes support both static code and dynamic code.
Summary
Since C # team does not want to add the index attribute to C # (although VB supports it), you can refer to the large interview video about Anders, it was forced to be added in the later stage because it required better support for com. Therefore, this feature has not made any major changes within the compiler or CLR. It is just a syntactic sugar and has very limited implementation. First of all, it can only be used in COM. In ordinary C # code, you cannot declare and define this feature, so this also causes its use range to be quite limited, most people may not even be exposed. Secondly, it only makes the user look like calling the index attribute. In fact, the property is obtained through the get_x and set_x methods. Finally, for backward compatibility, you can continue to use the get_x () and set_x () Methods to operate on attributes.
I hope this article will help you understand the index attributes!
References:
1. com InterOP in C #4.0: Indexed properties Author: samng, address: http://blogs.msdn.com/samng/archive/2009/11/03/com-interop-in-c-4-0-indexed-properties.aspx