ジェネリックスでこの動作を説明できる人はいますか? C#Generics関数
は、私は私がチェックする「コントロールでテキストボックス」タイプをやっているスイッチケースを使用することができ、サイドノートではC#のprotected virtual void LoadFieldDataEditor <T> (ref T control, string strFieldName) where T : Control
{
//T can be different types of controls inheriting from System.Web.UI.Control
if (control is TextBox)
{
//This line gives an error
//((TextBox)control).Text = "test";
//This line works!
(control as TextBox).Text = "Test";
}
}
での一般的な機能を持っていますか?
編集:
エラーメッセージを追加する忘れました!
ここに行く:
Error 3 Cannot convert type 'T' to 'TextBox'
EDIT:私たちはジェネリックについて話している間
を、私は別の質問を持っています。かなりトリッキーです方法は、他の一般的なタイプ
protected virtual void LoadFieldDataEditor <T1, T2> (T1 control, T2 objData, string strFieldName) where T1 : Control where T2 : BaseDataType
{
//I will need to access field1.
//I don't know at compile time if this would be SomeType1 or
//SomeType2 but all of them inherit from BaseDataType.
//Is this possible using generics?
}
public abstract class BaseDataType {}
public class SomeType1 : BaseDataType
{
string field1;
string field2;
}
エラーが何を意味するのかを尋ねるときは、エラーの内容を伝えることが非常に役立ちます。ちょうど今再現しようとしています... –
何がエラーですか? –
@Jon Skeet:くそー...私はあなたが事件にあったとは思わなかった... –