An interesting difference between C# and C++/CLI
2008-06-07 01:22
323 查看
Considering code below:
Why is there this diversity? As we know, all .net program would output to IL code, so the code and its metadata should be same as each other, but why the get accessor is available in C++/CLI but invalid in C#? In addition, is it valid if from perspective of CLR?
Then I used IL disasembler to get IL of above code and found that B class includes a Val property definition with get accessor, D includes a Val property definition with set accessor, and get and set methods are marked as hide sig respectively. d->Val in C++/CLI is compiled to code like below:
I guessed C++/CLI compiler try its best to query get method, so it invokes base class's in case of no get method definition in child class's metadata, while C# prefer treating the property as an integrity, so it is invalid when compiler find the property without get accessor hides base on.
The fact that child class doesn't include get definition can be proven by reflection:
We can get the same result if using C++/CLI as expected, since reflection is independent with concrete languages.
Conclusion:
As a basic grammar in C# and CLR, property is considered as an elements, which include one or two methods definition but not equal to methods. However, since standard c++ doesn't define property, C++/CLI designer may prefer treating it as one or two seperate methods. This case demonstrates the different philosophy between two languages in the face of the same issue, interesting!
// Component code: class B { public int Val { get {return 1;} } } class D : B { public new int Val { set {value;} } } // Client code: // C# D d = new D(); int i = d.Val; // compile error // C++/CLI D^ d = gcnew D(); int i = d->Val; // ok
Why is there this diversity? As we know, all .net program would output to IL code, so the code and its metadata should be same as each other, but why the get accessor is available in C++/CLI but invalid in C#? In addition, is it valid if from perspective of CLR?
Then I used IL disasembler to get IL of above code and found that B class includes a Val property definition with get accessor, D includes a Val property definition with set accessor, and get and set methods are marked as hide sig respectively. d->Val in C++/CLI is compiled to code like below:
int i = d->B::Val::get()
I guessed C++/CLI compiler try its best to query get method, so it invokes base class's in case of no get method definition in child class's metadata, while C# prefer treating the property as an integrity, so it is invalid when compiler find the property without get accessor hides base on.
The fact that child class doesn't include get definition can be proven by reflection:
// C# D d = new D(); int i = (int)typeof(B).GetProperty("Val").GetValue(d); //ok i = (int)typeof(D).GetProperty("Val").GetValue(d); // an exception will be thrown
We can get the same result if using C++/CLI as expected, since reflection is independent with concrete languages.
Conclusion:
As a basic grammar in C# and CLR, property is considered as an elements, which include one or two methods definition but not equal to methods. However, since standard c++ doesn't define property, C++/CLI designer may prefer treating it as one or two seperate methods. This case demonstrates the different philosophy between two languages in the face of the same issue, interesting!
相关文章推荐
- In SQL, what’s the difference between a full join and an inner join?
- What is the difference between an EXE and a DLL?
- The difference between an Isolated host and an In-Process host...
- Differences Between C++ Templates and C# Generics (C# Programming Guide)
- WHAT'S THE DIFFERENCE BETWEEN AN OEM AND AN ODM?
- Differences Between C++ Templates and C# Generics
- Differences Between a BI/Data Warehouse System and an OLTP System
- (C#) What is the difference between "const" and "static readonly" ?
- What’s the difference between an interface and an abstract class in Java?
- In C++, what’s the difference between an inline function and a macro?
- The difference of overriding between C# and Java
- 【转载】#445 - Differences Between an Interface and an Abstract Class
- an important difference between while and foreach on Perl
- C# - The differences between SendKeys.Send and SendKeys.SendWait
- the difference between an embOS interrupt and a zero latency interrupt
- Difference between event.target and event.currentTarget properties in an event object.
- The Similarities and Differences Between C# and Java -- Part 1(译)
- Understanding the Difference Between an Argument and a Parameter
- the difference between a material and an effect
- The Similarities and Differences Between C# and Java -- Part 1(译)