简体   繁体   English

访问可能不可用的第三方团体

[英]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. ThirdPartyClass类可能可用,也可能不可用。 It has one static variable myInt . 它具有一个静态变量myInt

int someInt;
if(ThirdPartyClass is available) // pseudo-code
{
  someInt = ThirdPartyClass.myInt;
} else {
  someInt = 0;
}

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM