Access 3rd Party Class That May Not Be Available

I need to access third-party classes that may or may not be available. How can we handle a situation like this?

For example:

Class ThirdPartyClass 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;
}

Answers


You are basically talking about reflection. I am assuming we do not need to try to auto-discover assemblies.

You can do this something along the lines of:

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;

Need Your Help

How to parse @name of license from JSON

jquery json each

I have the following JSON that I need to pull @name

How to find out who delete a branch at git?

git branch blame

I work at the team which works with git repository. We have an origin and several local copies. Everybody interacts with origin by different means: from Unix, from Windows, Unix shell, Windows shell,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.