File tree Expand file tree Collapse file tree 2 files changed +20
-2
lines changed
Expand file tree Collapse file tree 2 files changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -72,7 +72,16 @@ int main() {
7272
7373 try {
7474 auto cruncher = server->GetNumberCruncher ();
75- double pi = cruncher->ComputePi ();
75+ // try to cast to INumberCruncher2 to check if the new interface is implemented
76+ MyInterfaces::INumberCruncher2Ptr cruncher2 = cruncher;
77+ double pi = 0 ;
78+ if (cruncher2) {
79+ // use new interface
80+ pi = cruncher2->ComputePi2 ();
81+ } else {
82+ // fallback to old interface
83+ pi = cruncher->ComputePi ();
84+ }
7685 std::wcout << L" pi = " << pi << std::endl;
7786
7887 auto callback = CreateLocalInstance<MyClient>();
Original file line number Diff line number Diff line change @@ -13,7 +13,16 @@ static void CommunicateWithServer()
1313
1414 {
1515 var cruncher = server . GetNumberCruncher ( ) ;
16- double pi = cruncher . ComputePi ( ) ;
16+ double pi = 0 ;
17+ try {
18+ // try to cast to INumberCruncher2 to check if the new interface is implemented
19+ var cruncher2 = ( MyInterfaces . INumberCruncher2 ) cruncher ;
20+ // use new interface
21+ pi = cruncher2 . ComputePi2 ( ) ;
22+ } catch ( System . InvalidCastException ) {
23+ // fallback to old interface
24+ pi = cruncher . ComputePi ( ) ;
25+ }
1726 Console . WriteLine ( $ "pi = { pi } ") ;
1827
1928 // release reference to help GC clean up (not strctly needed)
You can’t perform that action at this time.
0 commit comments