特定のアクションでPOCOエンティティの特定の属性を更新する必要があります。
次のインターフェイスを定義しました。
public interface IEntityPOCO
{
Guid Id { get; set; }
}
public interface IHasLastChange : IEntityPOCO
{
DateTime LastChange { get; set; }
}
アクションメソッドの例を次に示します。
public void SomeStuff<T>(T entity) where T : class, IEntityPOCO
{
entity.Id = Guid.NewGuid(); // Works like a charm.
if (entity is IHasLastChange)
{
entity.LastChange = DateTime.Now; // Doesn't work. D'oh.
(IHasLastChange)entity.LastChange = DateTime.Now; // Doesn't work either.
}
}
- POCOによって実装される可能性のあるさまざまなプロパティ(すべて対応するインターフェイスを備えたもの)が非常に多いため、さまざまな関数シグネチャで問題を解決することはほとんど問題外です。
- パフォーマンス上の理由から、リフレクションは使用しません。
私の悲惨さから私を追い出すための実行可能な方法はありますか?