ジェネリック型を使用してそれをベースフォームにキャストすることを理解し、理解するのは苦労しています。下のコードは私が持っている構造を示しています。ジェネリック型キャストと割り当て可能なタイプ
public interface IBaseRepository : IDisposable
{
//stuff defined
}
public class BaseRepository : IBaseRepository
{
//stuff I want to access
}
public interface IBaseController<TRepo> where TRepo : IBaseRepository
{
TRepo Repository { get; }
}
public abstract partial class BaseController<TRepo>
: Controller, IBaseController<TRepo>
where TRepo : IBaseRepository
{
public virtual TRepo Repository { get; protected set; }
}
public interface ISpecificRepository : IBaseRepository
{
}
public class SpecificRepository : BaseRepository, ISpecificRepository
{
}
public class SpecificController : BaseController<ISpecificRepository>
{
}
// The below cast will only work if the controller
// specifically implements IBaseController<IBaseRepository>
var _controller = filterContext.Controller as IBaseController<IBaseRepository>
それが特異的BaseRepository具象クラスではなく継承したクラスのいずれかを実装していない限り、私はIBaseControllerにコントローラをキャストすることはできません。
この例を使用すると、コントローラクラスがSpecificControllerの場合、BaseRepositoryのメソッドとプロパティにどのようにアクセスできますか?
私は何をしようとしているのかについてより具体的に質問を編集しました。
SpecificController:
し、最終的に
はそうあなたのコードを使用します。 BaseControllerにはRepositoryプロパティがあります。したがって、このプロパティを介してリポジトリにアクセスしてください。 – CodeFuller
@ CodeFuller申し訳ありません私は私の質問で非常に明確ではなかった。私はそれを更新しました。うまくいけば、私が苦労している場所についてより具体的です。 – Guy
あなたの 'IBaseController'が共変--IBaseController ' –
Evk