いいえ。これは不可能です。マークアップを使用してユーザーコントロールを動的にロードするには、ASCX仮想パスを指定する必要があり、仮想パスのタイプの内部マッピングはありません。
ただし、私はまだ怠惰なので、これは私が使用したアプローチであり、依然として「タイプセーフ」であり、解決する問題をコード内の1つの場所に分離します。それでも「Pageオブジェクト」にアクセスする必要がありますが、それ以外の場合はばかげた詳細を処理します。
簡単な説明は次のとおりです。
- タイプを使用して、ヒューリスティックを使用して相対(ルートに対する)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;
}
}
}
}
}
}