Access to a third party class that cannot be accessed

I need to access third-party classes, which may or may not be available. How can we deal with this situation?

For instance:

The ThirdPartyClass class may or may not be available. It has one static variable myInt.

int someInt; if(ThirdPartyClass is available) // pseudo-code { someInt = ThirdPartyClass.myInt; } else { someInt = 0; } 
+4
source share
1 answer

You are mostly talking about reflection. I assume that we do not need to try to automatically detect assemblies.

You can do it something like:

 Type t = Type.GetType("<fullyqualifiedname>.ThirdPartyClass", false) if (t != null) { FieldInfo fi = t.GetField("myInt", BindingFlags.Public | BindingFlags.Static); someInt = (int)fi.GetValue(null); } else someInt = 0; 
+6
source

Source: https://habr.com/ru/post/1433245/


All Articles