Generic <T>怎么演员?

Kri*_*s-I 6 c# generics casting ioc-container

我有一个"Product"基类,其他一些类"ProductBookDetail","ProductDVDDetail"继承自这个类.我使用ProductService类对这些类进行操作.但是,我必须根据类型(书的ISBN,DVD的语言)进行检查.我想知道投射"productDetail"值的最佳方法,我在SaveOrupdate中收到.我尝试了GetType()并使用了(ProductBookDetail)productDetail但这不起作用.

谢谢,

var productDetail = new ProductDetailBook() { .... };
var service = IoC.Resolve<IProductServiceGeneric<ProductDetailBook>>();
service.SaveOrUpdate(productDetail);

var productDetail = new ProductDetailDVD() { .... };
var service = IoC.Resolve<IProductServiceGeneric<ProductDetailDVD>>();
service.SaveOrUpdate(productDetail);


public class ProductServiceGeneric<T> : IProductServiceGeneric<T>
{
    private readonly ISession _session;
    private readonly IProductRepoGeneric<T> _repo;
    public ProductServiceGeneric()
    {
        _session = UnitOfWork.CurrentSession;
        _repo = IoC.Resolve<IProductRepoGeneric<T>>();
    }
    public void SaveOrUpdate(T productDetail)
    {            
        using (ITransaction tx = _session.BeginTransaction())
        {
            //here i'd like ot know the type and access properties depending of the class
            _repo.SaveOrUpdate(productDetail);
            tx.Commit();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

log*_*cnp -2

使用:

if(productDetail is ProductDetailBook)
{
...
...
}
Run Code Online (Sandbox Code Playgroud)

对于其他人也是如此。