Company<IDesignation>
のオブジェクトを取得しています。今度はそれをICompany<Manager>
にキャストしたいと思います。実行時間私はIDesignation
が "マネージャ"タイプ以外のものであることを知っています。キャストコンクリート<Interface> to Interface <concrete>
答えて
これはあなたの探しているものですか?当社があると仮定すると、
Company comp = new Company();
Manager mgner = new Manager(comp.getManager());
IDesignation manager = mgner;
ICompany company = (ICompany)manager;
:
public class Company: ICompany, IDesignation //or something?
キャスト(uがやろうとしているもの)、または単にインターフェイスまたはオブジェクトをキャストのいずれかジェネリック型を使用するには、(明示的または暗示的にこのタスクを実行するかどうかに依存しますおそらくクラスにはそれをキャストするための関数があらかじめ定義されています)あなたのコメントが指摘したように...多分ユーザーや実行時の問題、あるいはあなたのオブジェクトをインスタンス化する必要があるかどうか、あなたがそれを実行したい方法で型キャスティングを使用するものを提供できるようにするために実装する必要があります。
パブリックインターフェースIEntityDelta
必要なものはContravariance
です。つまり、IEntityDelta
ジェネリックタイプのパラメータは反復的にする必要があります。 それを行うための唯一の方法と、このために働かなければならないことです。
public interface IEntityDelta<in T> : IEntityDelta where T : IEntity
注意をTで定義で。 あなたがそのインターフェイスの生みの親じゃないとIEntityDelta<>
は、W/O in
修飾子定義されている場合、あなたは運の出ている場合はin (Generic Modifier) (C# Reference)
またはこのUnderstanding Covariant and Contravariant interfaces in C#
をチェックしてください。
また、修飾子の追加は完了したよりも簡単であることに言及してください。あなたのメソッドをコンパイルするためには、プロパティなどは、その汎用タイプ(T)パラメータの逆分散(または「外」の場合の共分散)の条件を で満たす必要があります。
public interface IEntityDelta<in T> : IEntityDelta
where T : IEntity
{
void MakeDelta(T entity); // this is allowed
//T Entity { get; set; } // this won't work
}
public class EntityDelta<T> : IEntityDelta<T>
where T : class, IEntity
{
public T Entity { get; set; }
public EntityDelta(T entity) => Entity = entity;
public void MakeDelta(T entity) { }
}
public interface IEntityDelta { }
public abstract class Entity : IEntity { }
public class Order : Entity { }
public interface IEntity { }
: そして、あなたのクラスは、インタフェースがあなたの情報に基づいてどのように見えるか、これは(。ところでひどいものでした次回は、理にかなって、最小限のが、完全なコードを提供してもう少し時間 を捧げるする必要があります)
...と使用方法:私はC#でマネージャーの具体的な種類を保持ICompanyのタイプをインターフェイスに角括弧...... IDesignationのインターフェイスタイプを持株会社の具体的な種類を入力していくつかの問題を見るように
var order = new Order();
EntityDelta<IEntity> orderDelta = new EntityDelta<IEntity>(order);
IEntityDelta<IEntity> idelta = orderDelta;
IEntityDelta<Order> iOrderDelta = orderDelta;
- 1. キャスティングリスト<Class>リストへ<Interface>
- 2. Interface to
- 3. Javaジェネリック - クラス<Interface>
- 4. タスク<internface>をタスク<IEnumerable <Interface>>
- 5. LINQ to Entities to Interface Property
- 6. ダウンキャスティングshared_ptr <Base> to shared_ptr <Derived>?
- 7. Iterable <String> to Iterable <T>
- 8. バインドリスト<Dictionary <string、string >> to DropDownList
- 9. PCollection <Entity> to PCollection <TableRows>
- 10. Transform IEnumerable <Dto> to IEnumerable <ViewModel>
- 11. FSharpList <string> to IList <string> to XML
- 12. は<code>public interface Constants</code>内
- 13. Expression.PropertyOrField on Interface IList <T> throw InvalidOperationException
- 14. IList <IList<T>> to IReadonlyCollection <IReadonlyCollection >
- 15. ABAP:Loop At <itab> TO <wa><cond>
- 16. String to ArrayList <String>
- 17. Field <> With Linq to SQL
- 18. IList to IQueryable <T>
- 19. バインドリスト<DataTime> to DataGridヘッダー
- 20. シリアライゼーションQList <MyObject> to JSON
- 21. <A href> adding " to URL
- 22. Observable <Drawable> to simple Drawable
- 23. IQueryable <decimal> to decimal
- 24. Iterable <interface>戻り値のメソッドをIterable <?に代入するには? extends interface> return type method
- 25. ICollectionを<Interface>からICollectionに変換する<Class> [UWP] c#6.0
- 26. スウィフト/にObjC: `エヴァー<code>Xcode 6</code>ので、Interface Builderのから
- 27. ASP.NET MVC 3 List <T> to IEnumerable <SelectListItem>
- 28. onclick append <a> item</a> text to textarea
- 29. Java - List to HashMap <Long、Set <String>>
- 30. std :: array <float, 4> to std :: array <double, 4>キャスト
を修正(この会社のをICoにキャストmpany '')in C# –
お試しあればどうなりますか?問題が何であるかを示す[mcve]を投稿してください。 –
ああ、そうです。このクラスの継承を再加工したいと思うような音がします。あなたはコードの匂いの初期段階にいるように聞こえますが、間違っている可能性があります。私はあなたがそれをキャストする方法を見ていません – yardpenalty