Reflection to avoid System.Web reference
I have a shared reporting dll that is used in both windows and web. I am now trying to move the windows programs to .NET 4 client profile so need to avoid System.Web references.
At some points the code needs to get the directory of the website o开发者_开发问答r the directory of the dll. I used code something like this:
string path;
if (HttpContext.Current != null)
{
path = HttpContext.Current.Server.MapPath("default.aspx");
}
else
{
path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
Is there a way to use reflection or similar to avoid the System.Web reference so I can still get the appropriate path? If so how do I do it?
Any alternatives?
EDIT The reason I want to do this is I use a reporting system that can take a stylesheet file and apply it to reports (I do this for all reports). As a setting I previously had in the dll itself mystylesheet.repss.So if I wanted to change styles I just change it in the dll and it is applied to all reports. It was then just a case of dropping the appropriate repss file into the root directory on windows and the website. And finding the appropriate path to them.
Trying to use relative paths with the dll results in issues. Passing the report.\mystylesheet.repss works fine in Windows but trying ~/mystylesheet.repss .\mystylesheet.repss or anything else I can think of from the dll in web ends up looking in the wrong directory "c:\windows\system32\inetsrv".
I could move the setting out to each different windows and web app and pass it in with the full path worked out it seems backwards to do it that way when it is really an internal setting for the dll.
Hope that has all made sense.
Why don't you use paths relative to AppDomain.BaseDirectory.
This will be the root directory of the web application for ASP.NET, and the directory containing your executable for a Console or WinForms application.
For other application types, it will generally be a sensible default location: for example, in a VSTO 2005 application, it will be the directory containing the VSTO managed assemblies for your app, not, say, the path to the Excel executable.
If appropriate, you could support an optional configuration setting (e.g. appSetting) to allow the caller of your DLL to specify an alternate location, while defaulting to the base directory.
Another option is to allow your callers to specify a path to the stylesheet file, which can be absolute or relative. If relative, make it relative to AppDomain.BaseDirectory
.
if (!Path.IsPathRooted(stylesheetPath))
{
stylesheetPath = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
stylesheetPath);
}
...
Note that if you use a relative path, it will be relative to the current working directory, which can change during the lifetime of an application, and is not the same as the application base directory.
If the mechanism for determining the location should depend on the context, it sounds like it would be appropriate for the caller to pass it in as a constructor or method parameter or something similar. Whether that's just as a straight path or something like a Func<string, string>
as a "path resolver" will depend on exactly what you need to do.
精彩评论