我有一个HelperMethods包含静态方法的静态类。我需要在包含 的程序集中动态调用一个方法HelperMethods,但是这个程序集有时是可用的。所以我不能对包含我需要的方法的程序集进行引用。我也不能复制这个方法的代码,因为它会生成一个我需要的自定义对象SpecificServiceHost,ServiceHost但代码的另一部分只需要知道它是一个ServiceHost对象。如果SpecificServiceHost不可用,该方法只会创建一个常规ServiceHost对象。
private ServiceHost TryFindSpecialServiceHost()
{
try
{
var dynamicType = Type.GetType("Common, SuperApplication.Common.HelperMethods", true);
// Should find "public static SpecificServiceHost CreateSpecificServiceHost(Type serviceType, params Uri[] baseAddresses)" method
var createServiceHost = dynamicType.GetMethod("CreateSpecificServiceHost",
BindingFlags.Public | BindingFlags.Static,
null,
new[] { typeof(Type), typeof(Uri[]) },
null);
var dynamicInstance = createServiceHost.Invoke(null, new object[] { typeof(IMyWcfServiceContract), null });
return (ServiceHost) dynamicInstance;
}
catch
{
return new ServiceHost(typeof(IMyWcfServiceContract));
}
}
现在createServiceHost.Invoke抛出一个异常,该异常baseAddresses一定不是null由于ServiceHost框架中的实现。我认为该params论点将允许null引用。但事实并非如此!我应该怎么办?谢谢!