とコントロールの種類だから私は、次のクラスを持って...マッチ基底クラス
public partial class CommandBar : UserControl { .. }
...目標は、ユーザーがすべてのコントロールを再帰的に読み取り専用に設定できる汎用メソッドを作成することです。メソッドは無視するコントロールのList<Type>
を指定するオプションのパラメーターを提供する必要があります。このリストでは、このCommandBarをtypeof(CommandBar)
に渡して無視するようにしたいと考えています。
すべてが期待どおりに機能しますが、これらのタイプに一致する正しい方法を迷惑にしています。
以下を考慮してください。
Object o; // control to check, in this case the `CommandBarTop` object
Type t; // type to ignore
私はそれがこのように簡単であることが予想:
if (o is t){
// ignore
}
...しかし、私は、「一定の値が期待されている」構文例外を取得します。だから私は次のセットアップでそれを試した:
if (t == typeof(o)){
// ignore
}
それはコンパイルしましたが、期待どおりに動作しませんでした。この問題はタイプミスマッチと思われます。私は、次を取得し、デバッガを見てみる:
t => {Name = "CommandBar" FullName = "My.Name.Space.Controls.CommandBar"} System.Type {System.RuntimeType}
o => {ASP.controls_commandbar_ascx} object {ASP.controls_commandbar_ascx}
o.base
はタイプt
で実際にあるが、常におそらくないだろう最初のすべてのそれはアクセスできず、第2の方法は、一般的なあるべき、と一致するように基本型をチェックします私がしたいことをやりなさい。
ASP.NETが実行時にコントロールラッパーを生成し、それがユーザーに送信されると仮定します。この前提は、デバッガに表示されるアセンブリコードベースに基づいています。それは、次の言葉:
t.Assembly.CodeBase => "file:///.../bin/My.Name.Space.Project.DLL" string
o.GetType().Assembly.CodeBase => "file:///C:/Windows/Microsoft.NET/Framework/.../Temporary ASP.NET Files/root/.../App_Web_....DLL" string
私はまたタイプのGUIDを照合しようとしたが、彼らは基本的にはどちらか動作しません同じタイプではないので。
EDIT 1
私はあなたを再帰的に
今、私の質問に来public static void SetControlRecursivelyReadOnly(Object control, Boolean readOnly, IEnumerable<Type> controlTypesToIgnore = null)
{
if (null == control)
{
return;
}
new List<KeyValuePair<Type, String>>
{
// define all types which are relevant to access possible child controls
new KeyValuePair<Type, String>(typeof(ControlCollection), "Controls"),
new KeyValuePair<Type, String>(typeof(TableRow), "Rows"),
new KeyValuePair<Type, String>(typeof(TableCell), "Cells")
}.ForEach(x =>
{
// get defined property
Object property = typeof(Reflection).GetMethod("GetProperty")
.MakeGenericMethod(x.Key)
.Invoke(null,
new[]
{
control,
x.Value
});
// check if property is found and is IENumerable
if (!(property is IEnumerable))
{
return; // continues the foreach loop
}
// call recursive
foreach (Object o in (IEnumerable) property)
{
// <--- TODO CHECK IF CONTROL TYPE SHOULD BE IGNORED --->
SetControlRecursivelyReadOnly(o, readOnly);
}
});
// set relevant properties accordingly to readOnly parameter
new List<Tuple<PropertyInfo, Boolean>>
{
new Tuple<PropertyInfo, Boolean>(control.GetType().GetProperty("ReadOnly"), readOnly),
new Tuple<PropertyInfo, Boolean>(control.GetType().GetProperty("EnableButtons"), !readOnly),
new Tuple<PropertyInfo, Boolean>(control.GetType().GetProperty("Enabled"), !readOnly)
}.Where(x => null != x.Item1)
.ToList()
.ForEach(x => x.Item1.SetValue(control, x.Item2, null));
}
を読み取り専用するためのコントロールを設定するための私の方法を示した場合、それが役立つかもしれないと思いました。誰もがこの問題を解決する方法を知っていますか?
ありがとうございます!
を動作するはず?たぶん "コントロール"(テキストボックス、ラベル...)?いくつかの例を追加できますか? – Emanuele
@Emanueleはい先生、私は、コントロールを意味します。私はそれに応じて私の質問を更新しました。あなたがTextBox、Label asoで与えた例。まさに私が意味するものです。また、パネル、テーブル、DropDownLists ..時々プロパティは、「コントロール」と呼ばれている、「行」または「セル」themselfsがあるOFC私は私が既に持っているページ –