我的网站总是一个 vb.net项目,里面有大量的页面和类(我不喜欢它)。
现在我需要使用外部C#应用程序中的一些classess。所以我使用所有遗留代码创建了一个新的 vb.net dll ,并在ASP.NET
网站和C#
应用程序中使用它。
可悲的是,我的一些classess从web.config
读取配置(其中两个是static
)。如果它们在外部dll文件中,则无法访问 web.config
文件,因此我想删除对它的任何引用,并在应用程序池加载时配置它们,在任何页面加载之前
这可能吗?我正在进行搜索而没有任何成功!
谢谢!
答案 0 :(得分:0)
这是依赖注入模式的一个例子
using System;
namespace ConsoleApplication1
{
class Program
{
public class NewConfigClass
{
public string MyConfigParameter {get;set;}
}
public class LegacyNeedsConfig
{
public string ConfigurableSetting {get;set;}
public LegacyNeedsConfig(NewConfigClass config)
{
this.ConfigurableSetting = config.MyConfigParameter;
}
}
static void Main(string[] args)
{
NewConfigClass config = new NewConfigClass();
config.MyConfigParameter = "hello world"; //read from web or app config
LegacyNeedsConfig legacy = new LegacyNeedsConfig(config);
Console.WriteLine(legacy.ConfigurableSetting);
}
}
}