在外部程序集中调用方法时加载外部web.config变量
问题描述:
我正在使用反射来调用外部程序集中的方法。外部类/方法在WCF数据服务中。尝试通过通过反射来调用它的方法时在外部程序集中调用方法时加载外部web.config变量
的WCF数据服务使用
<configSections>
<section name="myCustomSection" type="MyWcfService.MyCustomSection, MyWcfService" />
</configSections>
加载配置变量在WCF服务工作正常,但不是在web.config中的自定义配置部分加载信息,一个单独的应用程序。我试着把配置信息放在本地的app.config中,但是我得到了同样的错误。
这是在本地应用程序的代码:
Assembly assembly = Assembly.LoadFile
("C:\\MyProject\\MyWcfService.dll");
Type[] t = assembly.GetTypes();
foreach (var v in t)
{
if (v.Name == "MyType")
{
var instance = Activator.CreateInstance(v);
v.InvokeMember("MyMethod", BindingFlags.InvokeMethod, null, instance, null);
}
}
这是从外部组件(WCF服务),其产生错误的代码,
MyCustomSection configSection = ConfigurationManager.GetSection("myCustomSection")
as MyCustomSection ;
configSection快到了null - '未将对象引用设置为对象的实例'。
如果它在本地应用程序app.config而不是web.config中查找,那么在本地添加相同的配置信息应该可以工作,我想。
谢谢。
答
您可能需要注册AppDomain.CurrentDomain.TypeResolve事件。这个链接有一个如何使用它的例子。 http://msdn.microsoft.com/en-us/library/system.appdomain.typeresolve.aspx