不,这是不可能的;必须提供ASCX 虚拟路径以动态加载带有标记的用户控件,并且没有虚拟路径类型的内部映射。
但是,因为我仍然很懒惰,所以这是我使用的仍然是“类型安全”的方法,并将解决问题隔离到代码中的单个位置。它仍然需要访问“页面对象”,但会处理一些愚蠢的细节。
以下是简要说明:
- 使用该类型查找相对(到我的根)ASCX 路径,使用启发式将类型从命名空间映射到虚拟路径;允许指定手动映射的方法,并在指定时使用它
- 将类型的相对路径转为正确/完整的虚拟路径
- 使用虚拟路径加载控件
- 像什么都没发生一样继续
享受(我只是从我的项目 YMMV 中复制粘贴选择的部分):
/// <summary>
/// Load the control with the given type.
/// </summary>
public object LoadControl(Type t, Page page)
{
try
{
// The definition for the resolver is in the next code bit
var partialPath = resolver.ResolvePartialPath(t);
var fullPath = ResolvePartialControlPath(partialPath);
// Now we have the Control loaded from the Virtual Path. Easy.
return page.LoadControl(fullPath);
} catch (Exception ex)
{
throw new Exception("Control mapping failed", ex);
}
}
/// <summary>
/// Loads a control by a particular type.
/// (Strong-typed wrapper for the previous method).
/// </summary>
public T LoadControl<T>(Page page) where T : Control
{
try
{
return (T)LoadControl(typeof (T), page);
} catch (Exception ex)
{
throw new Exception(string.Format(
"Failed to load control for type: {0}",
typeof (T).Name), ex);
}
}
/// <summary>
/// Given a partial control path, return the full (relative to root) control path.
/// </summary>
string ResolvePartialControlPath(string partialPath)
{
return string.Format("{0}{1}.ascx",
ControlPathRoot, partialPath);
}
ControlResolver 的代码清单:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FooBar
{
class ControlResolver
{
const string BaseNamespace = "TheBaseControlNameSpace";
readonly IDictionary<Type, string> resolvedPaths = new Dictionary<Type, string>();
/// <summary>
/// Maps types to partial paths for controls.
///
/// This is required for Types which are NOT automatically resolveable
/// by the simple reflection mapping.
/// </summary>
static readonly IDictionary<Type, string> MappedPartialPaths = new Dictionary<Type, string>
{
{ typeof(MyOddType), "Relative/ControlPath" }, // No virtual ~BASE, no .ASXC
};
/// <summary>
/// Given a type, return the partial path to the ASCX.
///
/// This path is the path UNDER the Control Template root
/// WITHOUT the ASCX extension.
///
/// This is required because there is no mapping maintained between the class
/// and the code-behind path.
///
/// Does not return null.
/// </summary>
public string ResolvePartialPath (Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
string partialPath;
if (resolvedPaths.TryGetValue(type, out partialPath))
{
return partialPath;
} else
{
string mappedPath;
if (MappedPartialPaths.TryGetValue(type, out mappedPath))
{
resolvedPaths[type] = mappedPath;
return mappedPath;
} else
{
// Since I use well-mapped virtual directory names to namespaces,
// this gets around needing to manually specify all the types above.
if (!type.FullName.StartsWith(BaseNamespace))
{
throw new InvalidOperationException("Invalid control type");
} else
{
var reflectionPath = type.FullName
.Replace(BaseNamespace, "")
.Replace('.', '/');
resolvedPaths[type] = reflectionPath;
return reflectionPath;
}
}
}
}
}
}