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 @@ -80,7 +80,16 @@ int main() {
8080
8181 try {
8282 auto cruncher = server->GetNumberCruncher ();
83- double pi = cruncher->ComputePi ();
83+ // try to cast to INumberCruncher2 to check if the new interface is implemented
84+ MyInterfaces::INumberCruncher2Ptr cruncher2 = cruncher;
85+ double pi = 0 ;
86+ if (cruncher2) {
87+ // use new interface
88+ pi = cruncher2->ComputePi2 ();
89+ } else {
90+ // fallback to old interface
91+ pi = cruncher->ComputePi ();
92+ }
8493 std::wcout << L" pi = " << pi << std::endl;
8594
8695 auto callback = MyClient::Create ();
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